rain13 Posted March 25, 2011 Report Share Posted March 25, 2011 I tried ltrim() but it doesn't do the job.From string below I want to extract user and date, and then everything after end of comment tag. I just cant find functions for this. [comment user="username" date="1.jan.2011"]styles: [b]bold[/b] [i]italic[/i] [u]underline[/u] [lnk]http://google.com[lnk] [img=http://leandrovieira.com/projects/jquery/lightbox/photos/image1.jpg] ,, tags I can just replace with html versions of these tags, but how do I extract url from [lnk] tags?Thanks in advance. Link to comment Share on other sites More sharing options...
jeffman Posted March 25, 2011 Report Share Posted March 25, 2011 (edited) If username=" and date=" are guaranteed to exist only in the contexts where you expect it, you can user strpos to locate them. Using that location to calculate a starting index, you can also locate the closing quotation mark.It is also possible to use a regular expression to extract the data, and simpler too, but only if you understand regex. Edited March 25, 2011 by Deirdre's Dad Link to comment Share on other sites More sharing options...
rain13 Posted March 25, 2011 Author Report Share Posted March 25, 2011 Thanks , just one more question. after i have string pos.. how do i get string between pos1 and pos2? like username starts after 14th char and ends at 23th char. now how di I get text between those 2 offsets? Link to comment Share on other sites More sharing options...
thescientist Posted March 25, 2011 Report Share Posted March 25, 2011 http://php.net/manual/en/ref.strings.phpsubstr Link to comment Share on other sites More sharing options...
birbal Posted March 25, 2011 Report Share Posted March 25, 2011 (edited) i think using reguler expression based pursing is more reliable and good instead of using string functions.you may want to start regexp from hereyou can also use bbcode function of php.http://www.php.net/manual/en/ref.bbcode.php Edited March 25, 2011 by birbal Link to comment Share on other sites More sharing options...
rain13 Posted March 25, 2011 Author Report Share Posted March 25, 2011 (edited) Looks like i've got problem with strpos function parsecode($Code){$UserName = substr($Code,15,strpos($Code,'"',0));echo $UserName."<br>";$bbarray = array("[b]","[/b]","[i]","[/i]","[u]","[/u]");$htmlarray = array("<b>","</b>","<i>","</i>","<u>","</u>");return str_replace($bbarray,$htmlarray,$Code);} when i echo what parsecode returns I get this username" date="1[comment user="username" date="1.jan.2011"]styles: bold italic underline [lnk]http://google.com[lnk] [img=http://leandrovieira.com/projects/jquery/lightbox/photos/image1.jpg] why it doesnt cut string after username?@birbal for somereason your's stringregexp link is broken. regexp looks bit too hard for me. could you give me regexp example about link tag? then I could use that example to build image code myself, and i could also use it later on other tags. Edited March 25, 2011 by SoItBegins Link to comment Share on other sites More sharing options...
birbal Posted March 25, 2011 Report Share Posted March 25, 2011 sorry i misspedled the link.i had fixed it. you can check for that link for complete tutorial and also look for backrefference group capture and lookaround (to work with bbcode). i think regexp should not be avoided. Link to comment Share on other sites More sharing options...
birbal Posted March 25, 2011 Report Share Posted March 25, 2011 more links which may be usefullhttp://www.zytrax.com/tech/web/regex.htmhttp://www.tuxradar.com/practicalphp/4/8/2 there is a good tutorial Link to comment Share on other sites More sharing options...
rain13 Posted March 25, 2011 Author Report Share Posted March 25, 2011 (edited) Looks like regexp is to find out if string matches with criteria, but still how do i pick string out. I mean if I know that string contasins lnk tags, then I still don't know whats between those tags.Is there betterway than this to get $UserName and $PostDate from string? [comment user=username" date="1.jan.2011] function parsecode($Code){$UserName = substr($Code,15);$UserEnd = strpos($UserName,'"');$UserName = substr($UserName,0,$UserEnd );$DateStart = strpos($Code,'date="');$PostDate = substr($Code, $DateStart+6 );$DateEnd = strpos($PostDate,'"');$PostDate = substr($PostDate, 0 ,$DateEnd);echo $UserName."<br>";echo $PostDate."<br>";$TextStart= $DateStart+6+$DateEnd+2;$Code = substr($Code , $TextStart);$bbarray = array("[b]","[/b]","[i]","[/i]","[u]","[/u]");$htmlarray = array("<b>","</b>","<i>","</i>","<u>","</u>");return str_replace($bbarray,$htmlarray,$Code);} here http://www.php.net/manual/en/function.bbcode-create.php the have regexp example but after reading those tutorials I cant understand this:'`\[(.+?)=?(.*?)\](.+?)\[/\1\]`'Ok I copied they're code but I dont understand a single thing in while loop function parsecode($Code){$UserName = substr($Code,15);$UserEnd = strpos($UserName,'"');$UserName = substr($UserName,0,$UserEnd );$DateStart = strpos($Code,'date="');$PostDate = substr($Code, $DateStart+6 );$DateEnd = strpos($PostDate,'"');$PostDate = substr($PostDate, 0 ,$DateEnd);echo $UserName."<br>";echo $PostDate."<br>";$TextStart= $DateStart+6+$DateEnd+2;$Code = substr($Code , $TextStart);$bbarray = array("[b]","[/b]","[i]","[/i]","[u]","[/u]");$htmlarray = array("<b>","</b>","<i>","</i>","<u>","</u>");$Code = str_replace($bbarray,$htmlarray,$Code); while (preg_match_all('`\[(.+?)=?(.*?)\](.+?)\[/\1\]`', $Code, $matches)) foreach ($matches[0] as $key => $match) { list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]); switch ($tag) { case 'size': $replacement = "<span style=\"font-size: $param;\">$innertext</span>"; break; case 'color': $replacement = "<span style=\"color: $param;\">$innertext</span>"; break; case 'center': $replacement = "<div class=\"centered\">$innertext</div>"; break; case 'quote': $replacement = "<blockquote>$innertext</blockquote>" . $param? "<cite>$param</cite>" : ''; break; case 'url': $replacement = '<a href="' . ($param? $param : $innertext) . "\">$innertext</a>"; break; case 'img': list($width, $height) = preg_split('`[Xx]`', $param); $replacement = "<img src=\"$innertext\" " . (is_numeric($width)? "width=\"$width\" " : '') . (is_numeric($height)? "height=\"$height\" " : '') . '/>'; break; case 'video': $videourl = parse_url($innertext); parse_str($videourl['query'], $videoquery); if (strpos($videourl['host'], 'youtube.com') !== FALSE) $replacement = '<embed src="http://www.youtube.com/v/' . $videoquery['v'] . '" type="application/x-shockwave-flash" width="425" height="344"></embed>'; if (strpos($videourl['host'], 'google.com') !== FALSE) $replacement = '<embed src="http://video.google.com/googleplayer.swf?docid=' . $videoquery['docid'] . '" width="400" height="326" type="application/x-shockwave-flash"></embed>'; break; } $Code = str_replace($match, $replacement, $Code); }return $Code;} what does this do?$matches[0] as $key => $matchas=>and this array($matches[1][$key], $matches[2][$key], $matches[3][$key]); Does this create array of other arrays? Edited March 25, 2011 by SoItBegins Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now