removing tags

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Alicia
New php-forum User
New php-forum User
Posts: 94
Joined: Sun Sep 21, 2003 5:30 pm

removing tags

Postby Alicia » Fri Nov 28, 2003 8:53 pm

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.

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Sat Nov 29, 2003 1:00 am

Code: Select all

<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: Select all

<?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: Select all

<?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.


Return to “PHP coding => General”

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron