473,809 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

preg_match - how to match \x00 (null)?

Hi all,

I'd like to work with a binary file....

I read the whole file into variable (it conatains binary data from 00-FF):

$fp=fopen($cfg, "r");
$content=fread( $fp,filesize($c fg));
fclose($fp);

// and then I'd like to check if it contains special binary sequence, let's say bytes "00 01
02 03":

$replacestring= "/\x00\x01\x02\x0 3/im";

if (preg_match($re placestring,$co ntent)==0) {
echo("bad input file format");
exit();
}

but i get error message:

<b>Warning</b>: preg_match(): No ending delimiter '/' found in <b>test.bat</b> on
line <b>57</b><br />

I think it is maybe because php uses strings terminated with null, but can i force php to
use strings with predefined length for example? Is there a way to work with strings
containing null characters?

Thank you in advance.

Y.
Jun 6 '06 #1
4 11632
Yandos wrote:
Hi all,

I'd like to work with a binary file....

I read the whole file into variable (it conatains binary data from 00-FF):

$fp=fopen($cfg, "r");
$content=fread( $fp,filesize($c fg));
fclose($fp);

// and then I'd like to check if it contains special binary sequence, let's say bytes "00 01
02 03":

$replacestring= "/\x00\x01\x02\x0 3/im";

if (preg_match($re placestring,$co ntent)==0) {
echo("bad input file format");
exit();
}

but i get error message:

<b>Warning</b>: preg_match(): No ending delimiter '/' found in <b>test.bat</b> on
line <b>57</b><br />

I think it is maybe because php uses strings terminated with null, but can i force php to
use strings with predefined length for example? Is there a way to work with strings
containing null characters?

Thank you in advance.

Y.


The PCRE library does expect the pattern to be a null-terminate string.
It is capable of looking for \0 though. Instead of the double-quoted
string, use single-quotes instead:

$replacestring= '/\x00\x01\x02\x0 3/im';

Now PCRE will see the escape sequences and correctly compile the regexp.

Jun 6 '06 #2
On Tue, 06 Jun 2006 14:23:24 -0700, Chung Leong wrote:
$replacestring= '/\x00\x01\x02\x0 3/im';


Why do you use multi-line matching and what will ignoring case buy you
here?

--
http://www.mgogala.com

Jun 7 '06 #3
"Chung Leong" <ch***********@ hotmail.com> wrote in
news:11******** *************@h 76g2000cwa.goog legroups.com:

The PCRE library does expect the pattern to be a null-terminate
string. It is capable of looking for \0 though. Instead of the
double-quoted string, use single-quotes instead:

$replacestring= '/\x00\x01\x02\x0 3/im';

Now PCRE will see the escape sequences and correctly compile the
regexp.


Works great, thank you!

Y.

--
sorry for any ads below :(

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Jun 7 '06 #4
Mladen Gogala <go****@sbcglob al.net> wrote in news:pan.2006.0 6.07.07.58.37.7 53254
@sbcglobal.net:
On Tue, 06 Jun 2006 14:23:24 -0700, Chung Leong wrote:
$replacestring= '/\x00\x01\x02\x0 3/im';


Why do you use multi-line matching and what will ignoring case buy you
here?


Thanks for comment, that's perhaps not necessary, but I'm used to do regexps that way. I'm not a
regexp guru and once something did not worked when I have not used ignore case and multiline
switches, so now I'm using em everywhere - no harm in writing 2 more letters ;) )

Y.

--
sorry for any ads below :(

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Jun 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4698
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&amp;" preg_match_all ("|product=(\d{5,10})&amp;|i", $html, $out); $out = 3456789 preg_match ("|product=(\d{5,10})&amp;|i", $html, $out);
6
1743
by: lkrubner | last post by:
I apologize for my igorance of Regex. I can't get this function to work. It gives the following error message. function checkForFloatingPoint($input=false) { $pattern = "?(*\.+|+)"; $match = preg_match (pattern, subject); echo "The match is: $match "; return $match;
5
7564
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match, $res_description); if (!$valid_srch) { ...
6
10254
by: mantrid | last post by:
Hello Found this piece of code using preg_match to check file types during upload of files. $allowed_file_types = "(jpg|jpeg|gif|bmp|png)"; preg_match("/\." . $allowed_file_types . "$/i", $_FILES) I understand the basic preg_match but am confused as to how the string pattern part is working i.e. "/\." . $allowed_file_types . "$/i"
3
3373
by: fienen | last post by:
I am working on a script to handle a search query. In some instances, the query could come through as "isbn:%20#############" (where %20 is an encoded space and the colon is optional). Basically I want to strip off the ISBN portion and leave just the numbers if that is the case. Orignally I was trying $value = $_GET; if (preg_match("isbn:?%20", $value)) {
2
4257
by: JanDoggen | last post by:
function vldLicense($lic) { echo "called with lic: ". $lic . "<br>"; echo preg_match('', $lic) . "<br>"; if (preg_match('{4}-{4}-{4}-{4}', $lic) == 0) return false; return true; } gives me:
13
5378
by: chadsspameateremail | last post by:
I might have found a problem with how preg_match works though I'm not sure. Lets say you have a regular expression that you want to match a string of numbers. You might write the code like this: preg_match( '/^+$/', $TestString ); OK everything seems fine. However, did you know if you pass the following to preg_match: "12345\n" it will return that a match occurred?!? Even though the newline is not a valid character in our regular...
3
1887
by: Happy Face | last post by:
Hi, All, I encountered this strange problem while using function preg_match. The following is the php code. when I set the line: $text = str_repeat('*', 12500); preg_match will return 0 for the hard coded regular expression. but if I set $text to 12499, it will return 1 for a match. $text = str_repeat('*', 12499);
8
4002
by: Thomas Mlynarczyk | last post by:
Hello, I want to split a given string into tokens which are defined by regexes: // example tokens - a bit more complex in real $tokens = array( 'NUMBER' ='~^\d+~', 'NAME' ='~^+~', 'ANY' ='~^.~' ); // make sure there is always a match
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.