Malfunctions and Bugs of Regex in PHP 5

Hello,

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 1.6.2.8 2009/07/21 14:37:17 islandusurper
#  uc_store.install,v 1.9.2.6 2009/05/07 20:52:28 islandusurper
#  uc_store.install,v 1.9.2.7 2009/07/21 14:37:21 islandusurper
#  uc_store.install,v 1.9.2.8 2009/10/21 19:40:27 islandusurper
#  ubercart/uc_store/includes/uc_price.inc: n/a
#  uc_price.inc,v 1.1.2.5 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/uc_order_order_pane.inc:409,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/ca.admin.inc:48,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/ca.ca.inc:248,263,263,263,263,263,263,263; shipping/uc_flatrate/uc_flatrate.admin.inc:38,35,35,35,35,35,35,35; shipping/uc_shipping/uc_shipping.admin.inc:137,295,138,296,138,296,138,296,138,296,138,296,138,296,138,296; uc_order/uc_order.admin.inc:342,361,361,361,361,361,361,361; uc_order/uc_order.order_pane.inc:421,421,421,421,421,421,421,421; uc_store/uc_store.admin.inc:44,46,46,46,46,46,46,46; shipping/uc_weightquote/uc_weightquote.admin.inc:38,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("/#:(.*\
)*/","g",$data);  

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.

why?

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:

<?php
$data = "#: stuff";
echo preg_replace("/#:(.*\
)*/","g",$data);

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("/#:(.*\
)*/","g",$data);

crashes the server?

i thought i copied the code from the php.net 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: