tags:

views:

50

answers:

5

I have a string with this html:

<div>
  <span> 1 </span>
  <a href="#"> <span> 2 </span> </a>
  <a href="#"> <span> 3 </span> </a>
  <a href="#"> <span> 4 </span> </a>
  ....
</div>

how do I remove the <span> tags from inside the links (<a>) ?

A: 
$string = str_replace("<span>", "", $string);
$string = str_replace("</span>", "", $string);
nute
A: 

If you just want to remove span tags inside an a tag and your format is exactly what you posted then the following fits that spec.

$string = str_replace('<a href="#"> <span>', '<a href="#">', $string);
$string = str_replace("</span> </a>", "</a>", $string);

If the format changes at all, you may require a regular expression or use DomDocument to parse the HTML.

Jason McCreary
You're going to have some quotes issues. StackOverflow won't let me edit your answer ... How does one do that actually?
nute
@nute: I think you have to have 2000 reputation to edit other people's things, unless that thing is community wiki.
icktoofay
@nute, thanks. I just copied and pasted. Got to love syntax highlighting.
Jason McCreary
A: 

it seems like you are trying to remove the link from the current page. If so you might want to take a look at this tutorial. This can be accomplished 3 ways. Using javascript, css, or php and css. If so you may want to look at his tutorial.

Mowgli
A: 

Do you want to also remove the contents of the <span> tags? If so, try this:

$string = preg_replace("/<span[^>]+\>/i", "", $string);

Otherwise, the str_replace method that nute posted will work fine.

Arms
no, just the tags `<span>` and `</span>`, and only the ones from inside the links
Alex
never mind.. i managed to solve this using regex:`preg_replace('@\<a([^>]*)>\<span([^>]*)>(.*?)\<\/span>@i', '<a$1>$3', $string)`
Alex