Jump to content

How to remove or get X first chars from left side of string?


rain13

Recommended Posts

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

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...