Jump to content

Recommended Posts

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>';

Link to comment
https://forums.phpfreaks.com/topic/193068-regex-help-replacing-tags-in-a-string/
Share on other sites

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);

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>

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);

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

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.