Page 1 of 1
[SOLVED] - Getting n chars from a string containing tags
Posted: Sun Aug 22, 2004 10:48 pm
by anjanesh
How do I accomplish this ?
Code: Select all
<?php
$str="<FONT COLOR=maroon>1234567890</FONT>";
echo substr($str,0,5);
?>
The output is <FONT
The output should be 12345 in maroon colour.
Posted: Sun Aug 22, 2004 11:05 pm
by feyd
Code: Select all
echo substr(strip_tags($str),0,5);
Posted: Sun Aug 22, 2004 11:07 pm
by anjanesh
This will not output in maroon colour.
The resultant string should be $str="<FONT COLOR=maroon>12345</FONT>";
Posted: Sun Aug 22, 2004 11:13 pm
by feyd
what have you tried, other than substr? maybe some regex?
Posted: Sun Aug 22, 2004 11:17 pm
by anjanesh
No I just have substr in the code.
Posted: Sun Aug 22, 2004 11:25 pm
by feyd
instead of just giving you the answer, I'm going to nudge you in the direction of at least 1 solution..
using the regular expression replacement functions, you can capture the substring you wish.. then call the same function again with a different pattern and replacement to convert the original string into a receptical for the substring.
Posted: Sun Aug 22, 2004 11:27 pm
by anjanesh
Thanks. I was thinking there was a simpler way to have this done in substr - thats all.
Thanks
Posted: Sun Aug 22, 2004 11:50 pm
by feyd
it's not possible with the substr function by itself, without exacting knowledge of the composition of the string.. you'd need to either analyze the string to see where the tag(s) start and end to find the 5 contained characters, or do as I did for the solution, use regex to just pull the data out of the string.. the solution I cooked up is fairly simple..