Malfunctions and Bugs of Regex in PHP 5


I have been playing Regex in PHP for 2 days, It is full of bugs.

For example:

I can’t get regex to delete all the words after the #: sign in the following text.

I have tried:
$data = str_replace($regex,“”,$data);

with regex =
#: .*
and about 10 obvious combinations…

If i want to replace just "#: " it’s fine, but to replace any words in the same line, it’s impossible. i used RegExr too.

#  uc_stock.install,v 2009/07/21 14:37:17 islandusurper
#  uc_store.install,v 2009/05/07 20:52:28 islandusurper
#  uc_store.install,v 2009/07/21 14:37:21 islandusurper
#  uc_store.install,v 2009/10/21 19:40:27 islandusurper
#  ubercart/uc_store/includes/ n/a
#,v 2009/08/17 21:27:55 islandusurper

"POT-Creation-Date: 2009-12-27 22:59+0000\
"Content-Transfer-Encoding: 8bit\
"Plural-Forms: nplurals=2; plural=(n>1);\

#: <<<<<<<(((((((((((((uc_cart/uc_cart.module:621,795,1176,621,795,1176,621,795,1176; uc_catalog/uc_catalog.module:167,1186,210,781,217,797,217,797,217,797,167,1171,224,804,224,804,224,804,167,1171,224,804
msgid "Home"
msgstr "Accueil"

#: shipping/uc_flatrate/uc_flatrate.module:219,219,219; shipping/uc_shipping/uc_shipping.module:307,493,307,493,307,493; uc_order/,409,409; uc_order/uc_order.module:924,959,730,742,742,742,924,959,747,747,738,924,959,738; uc_roles/uc_roles.module:794,1017,998,998,998,794,1032,1032,1032,794,1032; uc_store/uc_store.module:564,1627,2551,731,1572,755,1630,755,1630,755,1630,564,1627,2559,774,1641,774,1641,774,1641,564,1627,2559,774,1641; ca/,60,152,373,718,50,62,156,391,779,50,62,156,391,779,50,62,156,391,779,50,62,156,391,779,50,62,156,391,779,50,62,156,391,779,50,62,156,391,779; ca/,263,263,263,263,263,263,263; shipping/uc_flatrate/,35,35,35,35,35,35,35; shipping/uc_shipping/,295,138,296,138,296,138,296,138,296,138,296,138,296,138,296; uc_order/,361,361,361,361,361,361,361; uc_order/,421,421,421,421,421,421,421; uc_store/,46,46,46,46,46,46,46; shipping/uc_weightquote/,38,38,38,38,38,38; shipping/uc_weightquote/uc_weightquote.module:222,222
msgid "Title"
msgstr ""

I don’t know what that stuff you’ve posted is, but the simple answer is that you can’t use str_replace with regular expressions. PHP has special functions for that. Use the preg functions, preg_replace being the one for replacements.

Make sure your regular expression is properly delimited. Favourite delimiters tend to be / or @

the thing is, that using preg_match for the last 6-7 hours lots of logical things didnt work in PHP that work in RegExr. I had regex like “Keyword (\S\s\S){7}” taking up several lines of code and giving irregular results, skipping linebreaks, ignoring commands like (ABC) and (?!ABC)… it was a really bad in PHP.

“Keyword (\S\s\S){7}” isn’t a valid regular expression in PHP. Post some examples of what you want to do, and some expressions you’ve tried. Make it clear and simple so it is easy to help you.

I’ve never had a problem with anything I’ve done in the preg_* functions. If you want to post some of the commands you’re trying we’ll take a look.

ok sorry for the mentioned errors, and thankyou!

Here is an example…

I put this line in my code:

      $data = preg_replace("/#:(.*\

I run the browser.

A window pops up saying:

"an unhandled win32 exception occured in httpd.exe(5588). visual basic just in time debugger, etc etc not enabled, can be enabled etc.


Er, that doesn’t sound like anything to do with PHP, more like your server.

I can’t really see anything wrong with your regular expression, unless # needs to be escaped (I think it might). How about some sample data to compare with as well?

I don’t get that error on a mac or linux box. Sounds like a config issue.

As for the output I tried:

$data = "#: stuff";
echo preg_replace("/#:(.*\

and got

g stuff

Thanks Raffles, i know what you mean. it s the first error popup window i ever got in the server from many flurries of pages.

why does this:

$data = preg_replace("/#:.*/","g",$data);

work ok

and this

$data = preg_replace("/#:(.*\\r)*/","g",$data);

whereas this:

$data = preg_replace("/#:(.*\

crashes the server?

i thought i copied the code from the page on preg replace.

i’ll keep on working at it.

What do i write to take away all lines of text including linebreaks after the word #:

why do regex codes that work here:

not work in PHP?

i am all uppety about PHP not working tonight :slight_smile: its not allowed to be stronger than me :S

What do i write to take away all lines of text including linebreaks after the word #:

$originalString = "#: This is a test...";
$search = "#:";
if (false !== strpos($originalString, $search)) {
  $sLen = strlen($search);
  $found = strpos($originalString, $search);
  $newString = substr($originalString, $found + $sLen);

At least, this is what I would do. :smiley: