removing tags

A

Anonymous

Guest
Hi guys,

currently in my database, i have a column where they store bookmark html code
e.g: <a name="dddda"></a>

actually i wanna remove all all the html tags and only display the ddda in a textfield to the user.. the prob is how can i remove the html bookmark tags ?

I did think of using strip_tags function.. but what should i put at the end of the function ? is it <a name=""></a> ?? this doesn't make sense to me.. is there any other way i can do this ?


PLease advise.
 
Code:
<a name="dddda"></a>

Well, if you're quite certain that they're all going to look much like this, then you have a couple options. If you trust that there will always be exactly 9 characters before dddda and 6 after, you can just use substr():

Code:
<?php
$string = '<a name="dddda"></a>'; // you might trim() just in case

echo substr(9, -6);
/* output:
      dddda
*/
?>

If you're not so sure (interloping spaces? extraneous attributes?), it gets a little more complicated. If you're still sure that the string will only have one pair of quotation marks, it's still pretty simple. Just use strpos() to figure out where they are, and then substr() again to get everything between:

Code:
<?php
$string = '<a name="dddda"></a>';

$substr_start = strpos($string, '"') + 1;
// = 8 + 1 = 9
$substr_len = strpos($string, '"', $substr_start) - $substr_start;
// = 14 - 9 = 5

echo substr($string, $substr_start, $substr_len);
/* output:
      dddda
*/
?>

If the string is even more uncertain than that, then you're getting into a lot more complicated HTML parsing, which I won't go into just yet. But let us know if you do need it.
 
Back
Top