Skip to main content

Thread: PHP preg_replace not working as expected


i'm using following code apply html tags chat style collection of lines.
php code:
while ($char=mysql_fetch_assoc($chars)) {
    
$pattern="/%s:(.*?)([01]?[0-9]:[0-5][0-9])\\s*$/m";
    if (
$char['casesensitive']==0) { $pattern .= "i"; }
    
$pattern=sprintf($pattern$char['name']);
    
$replace=sprintf('%s:<span style="color:%s;">$1</span> $2'$char['name'], $char['color']);
    
$caption=preg_replace($pattern$replace$caption);

the database query , sprintf coded correctly , return proper values, , $caption defined before loop shown. have tested many times. have taken values have been passed preg_replace, , run them on local installation of php, replaces text expected. however, on webserver i'm attempting use on, pattern doesn't match , doesn't modify $caption @ all.
i'm attempting replace lines this:
html code:
<br>name: text colored. 8:47
with this:
html code:
<br>name:<span style="color:color;"> text colored.</span> 8:47
i'm using preg_replace several other times perform similar operations in same script, , works perfectly.
both installations of php version 5.3.8.

have tried preg_match( first.

if cant match, problem pattern not being correct.

tend use single quotes ' rather double quotes " when using regular expressions. unfortunately dont have regexbuddy on pc cant check pattern. in php quotes different , can have different behaviours.

also, should check see if preg_replace( retuning null) way can determine if pattern has become invalid.

preg_replace() returns array if subject parameter array, or string otherwise.

if matches found, new subject returned, otherwise subject returned unchanged or null if error occurred.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk PHP preg_replace not working as expected


Ubuntu

Comments

Popular posts from this blog

How to set the order of FAQs instead of alphabetical

Thread: Get UK Keyboard working

how do I change the e-mail address for my merchant account