Forum Moderators: coopster

Message Too Old, No Replies

Regular Expressions question

good with regex? help me please :)

         

mnet1

4:56 am on Jul 7, 2003 (gmt 0)

10+ Year Member



I've got a regex question, hopefully somebody can help :)

Basically, I want to convert a file with lines like this "<br>text_text:" to the following... "<br>Text Text:"

In simple terms, I need to find everything between "<br>" and ":" on all lines and replace it with a capitalised, underscore-free version of itelf.

Anybody know what preg_replace or similar I'd need to use to accomplish this?

vincevincevince

5:41 am on Jul 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$text=preg_replace("/\<br\>([^:])*:/","<br>".ucwords($1).":",$text)

(untested)

mnet1

10:11 am on Jul 7, 2003 (gmt 0)

10+ Year Member



edit: actually, i thought i knew what the problem with that statement was, but now i'm not sure. i just cant get it to work out at this point. will keep coding away, thanks again for your help :o)

vincevincevince

5:56 pm on Jul 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



sorry, forgot preg_replace wouldn't dynamically calculate replacements...

the below will work (assuming your start string is $text


$temp=$text;
while (preg_match("/\<br\>([^:]*):/",$temp,$matches))
{
$text=str_replace($matches[1],ucwords(str_replace("_"," ",$matches[1])),$text);
$temp=substr($temp,strlen($matches[1])+5);
}

mnet1

9:17 pm on Jul 8, 2003 (gmt 0)

10+ Year Member



Wow. What can I say!

It's no wonder I couldn't get it to work, I was working on such a simple level. That code is great and works without any trouble :)

Thanks very much, you've saved me a lot of headaches!