Jump to content

Searching/Replacing Strings


redbrad0

Recommended Posts

I have very large strings that I am needing to search for [color:xxxxxx] and replace it with <font color=xxxx> where the xxxx can be anything. So I created a while loop until I have found and replaced all [color:. The problem is that this script seems to run very slow. Is there a way to speed it up?

 

// Try and handle the [color:] tag
$tmpColorComplete = false;
$tmpI = 0;
while(!$tmpColorComplete)	{
$tmpColorIStart = strpos($htmlversion,"[color:");
if ($tmpColorIStart<0)	{
	$tmpColorComplete = true;
}	else	{
	$tmpColorIEnd = strpos($htmlversion,"]",$tmpColorI);
	$tmpColor = substr($htmlversion,($tmpColorIStart+7),($tmpColorIEnd-($tmpColorIStart+7)));

	$tmpHTMLStart = substr($htmlversion,0,$tmpColorIStart);
	$tmpHTMLEnd = substr($htmlversion,($tmpColorIEnd+1),strlen($htmlversion));

	$htmlversion = $tmpHTMLStart . "<font color=" . $tmpColor . ">" . $tmpHTMLEnd;

	$tmpColorComplete = false;
}

if ($tmpI>100)
	$tmpColorComplete = true;
$tmpI++;
}

Link to comment
https://forums.phpfreaks.com/topic/119516-searchingreplacing-strings/
Share on other sites

Freakin' thunderstorm wrecking my internet.. But here's my post at last.

 

efficacious is right, and below's a regex that should work. I guess you're looking for instances of [ color:* ] text to color [ /color ] (without spaces)? And the font tag is outdated, use a span tag instead, with CSS coloring:

 

<?php
function bb2html($text) {
$find = array(
	'~\[color:([#a-zA-Z0-9]*?)\](.*?)\[/color\]~s'
);
$replace = array(
	'<span style="color: $1;">$2</span>'
);
return preg_replace($find, $replace, $text);
}

//usage
$text = '[color:red]colored text[/color]';
echo bb2html($text);

//or
$text = '[color:#ff00ff]colored text[/color]';
echo bb2html($text);
?>

 

I wrote the function so it's easy to append new BB codes. It could be a bit simpler if you're only looking for the single [ color ] thingy.

good thinking but what if users choose to put a text based color name in as the value?

 

(3letrs)

 

That's not what I got out of his first post, but point taken.  Use:

<?php
$string = "[color:FFFFFF]Testing![/color]";
$string = preg_replace('#\[color:([#a-z0-9]+?)\](.+?)\[/color\]#i', '<font color="$1">$2</font>', $string);
echo $string;
?>

I tried to use both of these and although it replaced some code, it didn't replace all of them. Here are both samples. Any other ideas? Thanks for your help so far

 

$string_to_convert = "[h1]
[center][color:red]Some Title Here [color:black][i][u]Other Color[/color][/i][/u] with [u]Underlined[/u] Text![/h1][/center]
[/color]";
$htmlversion = test_EmailHTMLVersion($string_to_convert);
echo $htmlversion;

function test_EmailHTMLVersion($newText)	{
$htmlversion = $newText;

$htmlversion = str_replace("[b]","<b>",$htmlversion);
$htmlversion = str_replace("[/b]","</b>",$htmlversion);

$htmlversion = str_replace("[i]","<i>",$htmlversion);
$htmlversion = str_replace("[/i]","</i>",$htmlversion);

$htmlversion = str_replace("[u]","<u>",$htmlversion);
$htmlversion = str_replace("[/u]","</u>",$htmlversion);

$htmlversion = str_replace("[s]","<s>",$htmlversion);
$htmlversion = str_replace("[/s]","</s>",$htmlversion);

$htmlversion = str_replace("
[left]","<div align=left>",$htmlversion);
$htmlversion = str_replace("[/left]
","</div>",$htmlversion);

$htmlversion = str_replace("
[center]","<div align=center>",$htmlversion);
$htmlversion = str_replace("[/center]
","</div>",$htmlversion);

$htmlversion = str_replace("
[right]","<div align=right>",$htmlversion);
$htmlversion = str_replace("[/right]
","</div>",$htmlversion);

$htmlversion = str_replace("[h1]","<h1>",$htmlversion);
$htmlversion = str_replace("[/h1]","</h1>",$htmlversion);

$htmlversion = str_replace("[h2]","<h2>",$htmlversion);
$htmlversion = str_replace("[/h2]","</h2>",$htmlversion);

$htmlversion = str_replace("[h3]","<h3>",$htmlversion);
$htmlversion = str_replace("[/h3]","</h3>",$htmlversion);

$htmlversion = str_replace("[h4]","<h4>",$htmlversion);
$htmlversion = str_replace("[/h4]","</h4>",$htmlversion);

$htmlversion = str_replace("[h5]","<h1>",$htmlversion);
$htmlversion = str_replace("[/h5]","</h5>",$htmlversion);

$htmlversion = str_replace("[hr]","<hr>",$htmlversion);    	

// Try and handle the [color:] tag
$find = array('~\[color:([#a-zA-Z0-9]*?)\](.*?)\[/color\]~s');
$replace = array('<span style="color: $1;">$2</span>');
$htmlversion = preg_replace($find, $replace, $htmlversion);

//$htmlversion = preg_replace('#\[color:([a-z0-9]{6})\](.+?)\[/color\]#i', '<font color="$1">$2</font>', $htmlversion);

$htmlversion = str_replace("\r\n","<br>\r\n",$htmlversion);

return $htmlversion;
}

 

echo "<br><br>Test #2<br><br>";
$htmlversion = test_EmailHTMLVersion_2($string_to_convert);
echo $htmlversion;

function test_EmailHTMLVersion_2($newText)	{
$htmlversion = $newText;

$htmlversion = str_replace("[b]","<b>",$htmlversion);
$htmlversion = str_replace("[/b]","</b>",$htmlversion);

$htmlversion = str_replace("[i]","<i>",$htmlversion);
$htmlversion = str_replace("[/i]","</i>",$htmlversion);

$htmlversion = str_replace("[u]","<u>",$htmlversion);
$htmlversion = str_replace("[/u]","</u>",$htmlversion);

$htmlversion = str_replace("[s]","<s>",$htmlversion);
$htmlversion = str_replace("[/s]","</s>",$htmlversion);

$htmlversion = str_replace("
[left]","<div align=left>",$htmlversion);
$htmlversion = str_replace("[/left]
","</div>",$htmlversion);

$htmlversion = str_replace("
[center]","<div align=center>",$htmlversion);
$htmlversion = str_replace("[/center]
","</div>",$htmlversion);

$htmlversion = str_replace("
[right]","<div align=right>",$htmlversion);
$htmlversion = str_replace("[/right]
","</div>",$htmlversion);

$htmlversion = str_replace("[h1]","<h1>",$htmlversion);
$htmlversion = str_replace("[/h1]","</h1>",$htmlversion);

$htmlversion = str_replace("[h2]","<h2>",$htmlversion);
$htmlversion = str_replace("[/h2]","</h2>",$htmlversion);

$htmlversion = str_replace("[h3]","<h3>",$htmlversion);
$htmlversion = str_replace("[/h3]","</h3>",$htmlversion);

$htmlversion = str_replace("[h4]","<h4>",$htmlversion);
$htmlversion = str_replace("[/h4]","</h4>",$htmlversion);

$htmlversion = str_replace("[h5]","<h1>",$htmlversion);
$htmlversion = str_replace("[/h5]","</h5>",$htmlversion);

$htmlversion = str_replace("[hr]","<hr>",$htmlversion);    	

// Try and handle the [color:] tag
$htmlversion = preg_replace('#\[color:([a-z0-9]{6})\](.+?)\[/color\]#i', '<font color="$1">$2</font>', $htmlversion);

$htmlversion = str_replace("\r\n","<br>\r\n",$htmlversion);

return $htmlversion;
}

An aside from the main post: Utilize the fact that str_replace accepts arrays to clean up your code, or use regex. For example, preg_replace('/\[(\/?h\d)\]/', '<$1>', $str); will take care of all the h# tags, opening and closing. You can expand the h into a character class or alternation to handle the other tags.

I tried to look at that code on the linked thread but since I am not sure the exacts on regexp can you help me just change the [color:xxxx] to the span color? I can then just use the str_replace to replace the [/color] tag.

 

$string_to_convert = "[h1]
[center][color:red]Some Title Here [color:black][i][u]Other Color[/color][/i][/u] with [u]Underlined[/u] Text![/h1][/center]
[/color]";
$htmlversion = replace_colorHTML($string_to_convert);
echo $htmlversion;

function replace_colorHTML($newText)	{
$newText = preg_replace('#\[color:([a-z0-9]{6})\]#i', '<span style="color: $1;">', $newText);

return $newText;
}

Thanks guys for your quick replys for something that seems so easy. I guess after this I will need to start learning regexp.

 

DarkWater.. I tried your code and it doesnt seem to replace the color

 

$string_to_convert = "[h1]
[center][color:red]Some Title Here [color:black][i][u]Other Color[/color][/i][/u] with [u]Underlined[/u] Text![/h1][/center]
[/color]";
$htmlversion = replace_colorHTML($string_to_convert);
echo $htmlversion;

function replace_colorHTML($newText)	{
// Replace all the starting color tags
$newText = preg_replace('#\[color:([a-z0-9]{6})\]#i', '<span style="color: #$1;">', $newText);
$newText = str_replace('[/color]', '</span>', $newText);

return $newText;
}

Sorry, I thought you just wanted hex values in there, not color names.  Try:

 

function replace_colorHTML($newText) {

// Replace all the starting color tags

$newText = preg_replace('#\[color:([a-z0-9#]+?)\]#i', '<span style="color: $1;">', $newText);

$newText = str_replace('[/color]', '</span>', $newText);

 

return $newText;

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.