A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearWhy won't this standard Reg Ex work in PHP's ereg function - page 1
User InfoPosts
Why won't this standard Reg Ex work in PHP's ereg function#1
I am a bit of a newbie to Reg Ex and don't fully understand the difference between the different flavors. However, I have a basic Reg Ex that works when I try it via the UNIX system (vi and grep) but not when I try to to use it in PHP's ereg functions. I suspect there is something different about the PHP ereg function that is making this not work:

<?php
$string = 'Feugiat <em>hendrerit</em> sit iriuredolor aliquam.';
$string = ereg_replace("<em\b[^>]*>(.*?)</em>","\\1",$string);
echo $string;
?>


I would like this to output Feugiat hendrerit sit iriuredolor aliquam. without the em tags. However it just returns an empty string.

posted date: 2008-12-17 10:37:00


Re: Why won't this standard Reg Ex work in PHP's ereg function#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-17 10:37:01


Re: Why won't this standard Reg Ex work in PHP's ereg function#3
You may need to escape the backslash:$string = ereg_replace("<em\\b[^>]*>(.*?)</em>","\\1",$string);This is because \b in a PHP string means something different from a \b in a regular expression. Using \\ in the PHP string passes through a single backslash to ereg_replace(). This is the same reason you need to use double backslash in the replacement string "\\1".Depending on your application, you may also want to consider the possibility that your input $string does not contain any <em> tags. In that case, the above statements would result in an empty string, which is probably not what you intend.

posted date: 2008-12-17 10:39:00


Re: Why won't this standard Reg Ex work in PHP's ereg function#4
If removing <em> tags is your intention, I would recommend the following:<?php $string = 'Feugiat <em>hendrerit</em> sit iriuredolor aliquam.'; $string = ereg_replace("</?em\\b[^>]*>", "", $string); echo $string;?>Greg Hewgill is right about the escaping of backslashes in a PHP string. You need to do it to get a literal backslash into your regex pattern string.

posted date: 2008-12-17 10:52:00


Re: Why won't this standard Reg Ex work in PHP's ereg function#5
If all you're using the regular expression for is to remove the html tags, perhaps php's strip_tags() function would be more appropriate.php.net manual entry

posted date: 2008-12-17 10:53:00


Re: Why won't this standard Reg Ex work in PHP's ereg function#6
I have never understood ereg_ and always use preg. If you add the backslash like Greg suggests and change to preg_ it will compile.$string = preg_replace('%<em\\b[^>]*>(.*?)</em>%','\\1',$string);Edit: I agree with others here that this particular approach might not be ideal for the problem. But still, preg_ is most often the way to go when using regexes in PHP.

posted date: 2008-12-17 11:05:00


Re: Why won't this standard Reg Ex work in PHP's ereg function#7
Alternately, you could use single-quoted strings so that there's no interpretation of the contents of the string.

posted date: 2008-12-17 11:11:00


Re: Why won't this standard Reg Ex work in PHP's ereg function#8
ereg_replace does not support the word boundary assertion (\b) or non-greedy modifier (*?). PEZ is right, you should probably be using preg.preg_replace('!<em\\b[^>]*>(.*?)</em>!', '$1', $string)The extra backslash is not strictly necessary because PHP does not replace \b, but it is a good idea to always escape backslashes in a string literal.

posted date: 2008-12-17 11:16:00


Re: Why won't this standard Reg Ex work in PHP's ereg function#9
It's probably a good idea to avoid ereg for future compatibility. It looks like it's been depreciated in php6 according to this.The ereg extension, which supports Portable Operating System Interface (POSIX) regular expressions, is being removed from core PHP support.

posted date: 2008-12-17 11:43:00


Re: Why won't this standard Reg Ex work in PHP's ereg function#10
This doesn't work for me. See my new 'answer below'

posted date: 2008-12-17 12:24:00


Re: Why won't this standard Reg Ex work in PHP's ereg function#11
Greg Hewgill's answer seemed right at first but when I try and use this code from that answer:<?php $string = 'Feugiat <em>hendrerit</em> sit iriuredolor aliquam.';$string = ereg_replace("<em\\b[^>]*>(.*?)</em>","\\1",$string);echo $string;?>I get this error:Warning: ereg_replace() [function.ereg-replace]: REG_BADRPT in test.php on line 3So that ereg pattern doesn't seem to work just yet.

posted date: 2008-12-17 12:27:00


Re: Why won't this standard Reg Ex work in PHP's ereg function#12
Yeah, totally, but I was more just bringing this up as an example, b/c I didn't understand how PHP uses RegEx. And I can imagine a situation where I only want to remove one specific tag.

posted date: 2008-12-17 12:33:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.