jake2891 Posted February 23, 2010 Share Posted February 23, 2010 can anyone help me replace some tags in a string with regex. basically instead of putting <b><u><i> for bold , italics and underline. my html editor is putting <span style="font-weight:bold" etc. i need to search for these patterns and replace them with the <b><i><u> tags for use in xml. here is an example string. although sometimes its just bold and not italics etc. so determining on what to replace the closing span tag with is getting to me $test = ' please <span style="font-weight: bold; font-style: italic; text-decoration: underline;">replace me</span> then carry on here and <br><span style="font-weight: bold; font-style: italic;">replace me2</span> then carry on and <span style="text-decoration: underline;">replace me 3</span> then carry on and <span style="font-weight: bold;">replace me 4</span><br>'; Quote Link to comment https://forums.phpfreaks.com/topic/193068-regex-help-replacing-tags-in-a-string/ Share on other sites More sharing options...
sader Posted February 23, 2010 Share Posted February 23, 2010 check if this works $s = array("<b>", "<u>", "<i>", "</b>", "</u>", "</i>"); $r = array("<span style='font-weight:bold'>", "<span style='text-decoration:underline'>", "<span style='font-style:italic'>", "</span>"); echo str_ireplace($s, $r, $str); Quote Link to comment https://forums.phpfreaks.com/topic/193068-regex-help-replacing-tags-in-a-string/#findComment-1016759 Share on other sites More sharing options...
jake2891 Posted February 23, 2010 Author Share Posted February 23, 2010 thanks for the reply. i tried i this on my string i provided and it did not replace any of the tags? am i doing it incorrectly? $str = ' please <span style="font-weight: bold; font-style: italic; text-decoration: underline;">replace me</span> then carry on here and <br><span style="font-weight: bold; font-style: italic;">replace me2</span> then carry on and <span style="text-decoration: underline;">replace me 3</span> then carry on and <span style="font-weight: bold;">replace me 4</span><br>'; $s = array("<b>", "<u>", "<i>", "</b>", "</u>", "</i>"); $r = array("<span style='font-weight:bold'>", "<span style='text-decoration:underline'>", "<span style='font-style:italic'>", "</span>"); echo str_ireplace($s, $r, $str); Quote Link to comment https://forums.phpfreaks.com/topic/193068-regex-help-replacing-tags-in-a-string/#findComment-1016771 Share on other sites More sharing options...
jake2891 Posted February 23, 2010 Author Share Posted February 23, 2010 the values were in the wrong order . should have been echo str_ireplace($r, $s, $str); but still my string comes out a bit wrong it comes out like this? the whole string is underlined and only some bold tags get done rite. any ideas? thanks please <span style="font-weight: bold; font-style: italic; text-decoration: underline;">replace me</b> then carry on here and <br><span style="font-weight: bold; font-style: italic;">replace me2</b> then carry on and <u>replace me 3</b> then carry on and <b>replace me 4</b><br> Quote Link to comment https://forums.phpfreaks.com/topic/193068-regex-help-replacing-tags-in-a-string/#findComment-1016779 Share on other sites More sharing options...
sader Posted February 23, 2010 Share Posted February 23, 2010 OMG sory I mistunerstand ok I will try to make right code Quote Link to comment https://forums.phpfreaks.com/topic/193068-regex-help-replacing-tags-in-a-string/#findComment-1016800 Share on other sites More sharing options...
sader Posted February 23, 2010 Share Posted February 23, 2010 here's the code that I come up to: function replace_span_tag($match) { $out = $match[2]; $style_str = $match[1]; if(preg_match('/font-weight\\s*:\\s*bold/si', $style_str)) { $out = "<b>".$out."</b>"; } if(preg_match('/font-style\\s*:\\s*italic/si', $style_str)) { $out = "<i>".$out."</i>"; } if(preg_match('/text-decoration\\s*:\\s*underline/si', $style_str)) { $out = "<u>".$out."</u>"; } return $out; } $result = preg_replace_callback('/<span\\s+style="(.*?)">(.+?)<\/span>/si', 'replace_span_tag', $str); Quote Link to comment https://forums.phpfreaks.com/topic/193068-regex-help-replacing-tags-in-a-string/#findComment-1016813 Share on other sites More sharing options...
Psycho Posted February 23, 2010 Share Posted February 23, 2010 I think it would be better just to use an array of search/replace values instead of using multiple preg_replace() functions. Plus the function above didn't work for me. I have tested the following and it appears to work as you requested - although it may not be the most efficient $search = array( "/(<span style=\")(.*?)font-weight: bold;(.*?)(\">)(.*?)(<\/span>)/i", "/(<span style=\")(.*?)font-style: italic;(.*?)(\">)(.*?)(<\/span>)/i", "/(<span style=\")(.*?)text-decoration: underline;(.*?)(\">)(.*?)(<\/span>)/i", "/(<span style=\")(.*?) {2,}(.*?)(\">)/i", //remove duplicate spaces "/(<span style=\")( ){0,1}(\">)(.*?)(<\/span>)/i" //remove empty span tags ); $replace = array( "<b>$1$2$3$4$5$6</b>", "<i>$1$2$3$4$5$6</i>", "<u>$1$2$3$4$5$6</u>", "$1$2 $3$4", "$4" ); $str = 'please <span style="font-weight: bold; font-style: italic; text-decoration: underline;">replace me</span> then carry on here and <br><span style="font-weight: bold; font-style: italic;">replace me2</span> then carry on and <span style="text-decoration: underline;">replace me 3</span> then carry on and <span style="font-weight: bold;">replace me 4</span><br>'; $str = preg_replace($search, $replace, $str); echo $str; Output please <b><i><u>replace me</u></i></b> then carry on here and <br><b><i>replace me2</i></b> then carry on and <u>replace me 3</u> then carry on and <b>replace me 4</b><br> EDIT: Fixed bug with replacement of empty span tags Quote Link to comment https://forums.phpfreaks.com/topic/193068-regex-help-replacing-tags-in-a-string/#findComment-1016825 Share on other sites More sharing options...
sader Posted February 23, 2010 Share Posted February 23, 2010 very strange that it didn't work I did tested code before I was posting it and it worked. anyways this regexp '/<span\\s+style="(.*?)">(.+?)<\/span>/si' could be changed to '/<span\\s+style\\s*=\\s*"(.*?)"\\s*>(.*?)<\/span>/si' this should handle some extra whitespaces and also like mjdamato's regexp does pass <span></span> without inner html Quote Link to comment https://forums.phpfreaks.com/topic/193068-regex-help-replacing-tags-in-a-string/#findComment-1016843 Share on other sites More sharing options...
jake2891 Posted February 23, 2010 Author Share Posted February 23, 2010 thanks so much guys for all your help, much appreciated. ill give those examples a try now. Quote Link to comment https://forums.phpfreaks.com/topic/193068-regex-help-replacing-tags-in-a-string/#findComment-1016867 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.