preg_match to preg_replace

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi guys,

I have a sample string "[hp-ex][log]peter[hall o]" that I need to process. I
need to remove the [] and separate the sections with a space. If there are
more then one [] part the first part should be bold (add <b></b> around it).
I have a solution that is working very well, but am thinking that I can do
this with preg_replace directly, what do you think? The final output is
always found in $thestr. Can all this be done with one preg_replace?

<?php

echo "<pre>";

$str = "[hp-ex][log]peter[hall o]";

preg_match_all("/\[(.+?)\]/", $str, $matches);

print_r($matches);

$m1 = $matches[0];
$m2 = $matches[1];

if(count($m1)>1) {
	$m2[0] = "<b>$m2[0]</b>";
	$thestr = implode(" ", $m2);
} elseif(count($m1)==1) {
	$thestr = $m2[0];
} else $thestr = "";

echo $thestr;

echo "</pre>";

?>

Best regards,
Peter Lauri

www.dwsasia.com - company website
www.lauri.se - personal website

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux