FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » regex help
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: regex help [message #176238 is a reply to message #176237] Fri, 09 December 2011 11:55 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On Thu, 08 Dec 2011 22:51:56 -0500, Jerry Stuckle wrote:

> OK, guys, I'll be the first to admit my regex knowledge is more than
> limited. And I've never used ereg().
>
> So, I need to convert the following ereg() call in some code I'm
> modifying to a preg() call. Can someone please tell me what the ereg()
> does and the equivalent preg()?
>
> if (ereg("(error</b>:)(.+)(<br)", $buffer, $regs))

Hmmm

By comparing the manual for the two functions:

ereg returns false or 0 if no matches depending on the presence of the
third parameter, or the length of the matched string (which it puts in
$regs[0])

$regs[1], $regs[2] and $regs[3] contain the matched elements ie:

whole match of "/error<\/b>:.+<br/" -> $regs[0]
partial match of "/error<\/b>:/" -> $regs[1]
partial match of "/.+/" -> $regs[2]
partial match of "/<br/" -> $regs[3]
return value is strlen($regs[0])

preg_match returns 0 if no matches, or 1 if a match.

The matched sections for ereg are placed in the $regs array, similar to
$matches array in preg_match

I would suggest:

if (preg_match("|(error</b>:)(.+)(<br)|", $buffer, $regs))

might be equivalent.

Test:

<?php

$strings = array();
$strings[] = "<b>string error</b>: The string was broken<br />";
$strings[] = "<b>string error</b>: The string was broken<br>";
$strings[] = "string error: The string was broken<br />";
$strings[] = "string error: The string was broken<br>";

$regs = array();
$matches = array();

foreach ($strings as $string)
{
echo "test string: '{$string}'\n";
if (ereg("(error</b>:)(.+)(<br)", $string, $regs))
{
echo "ereg result:\n";
print_r($regs);
}
else
{
echo "ereg, no match\n";
}
if (preg_match("|(error</b>:)(.+)(<br)|", $string, $matches))
{
echo "preg_match result:\n";
print_r($matches);
}
else
{
echo "preg_match, no match\n";
}
}

?>

output:

$ php eregvspreg.php
test string: '<b>string error</b>: The string was broken<br />'
ereg result:
Array
(
[0] => error</b>: The string was broken<br
[1] => error</b>:
[2] => The string was broken
[3] => <br
)
preg_match result:
Array
(
[0] => error</b>: The string was broken<br
[1] => error</b>:
[2] => The string was broken
[3] => <br
)
test string: '<b>string error</b>: The string was broken<br>'
ereg result:
Array
(
[0] => error</b>: The string was broken<br
[1] => error</b>:
[2] => The string was broken
[3] => <br
)
preg_match result:
Array
(
[0] => error</b>: The string was broken<br
[1] => error</b>:
[2] => The string was broken
[3] => <br
)
test string: 'string error: The string was broken<br />'
ereg, no match
preg_match, no match
test string: 'string error: The string was broken<br>'
ereg, no match
preg_match, no match
$

I removed the following messages from the output for clarity:

PHP Deprecated: Function ereg() is deprecated in /home/denis/programming/
php/eregvspreg.php on line 15

You may want to add some test cases?

Rgds

Denis McMahon
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: <beg> ADODB PHP Extension Help </beg>
Next Topic: Edit a record?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 13 01:03:37 GMT 2024

Total time taken to generate the page: 0.05550 seconds