Jump to content
  • Advertisement
Sign in to follow this  
CelticSir

[web] String extraction in php

This topic is 3621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys and gals. Need some help with my php script. I just need a basic example so i can see how its done then i can apply it to my own script. Basically I am trying to cut a string into two segments. But the "divider" is not just a single symbol thus the explode function didn't work for me. Imagine this: $Var = '[ quote] Hello [/ quote] Yes hello there!'; (i had to put spaces due to the BBCode on this site). Now what I am trying to do is split it "after" the last char of [/ quote]. So that it then becomes: $Var = '[ quote] Hello [/ quote]'; $var2 = 'Yes hello there!'; Now is this possible in php, and if so how is it done?

Share this post


Link to post
Share on other sites
Advertisement
Of course it is possible. Two ways off the top of my head:

1) Use strpos() to find the last /quote and use substr() to create the new variables.

2) Use preg_split() to split the variable with a regular expression (slower than strpos/substr).

Share this post


Link to post
Share on other sites
If I understood it right, you're looking for a bbcode script? Or better, how to make some kind of bbcode. :)

here you go, create 2 arrays:

$str= "This is some [quote ]example[/quote ] text.";

$search = array(0=>"/\[quote\](.?)\[\/quote\]/isU");
$replace = array(0=>"< blockquote >$1< /blockquote >");

$str=preg_replace($search,$replace,$str);

This would give you something like This is some < blockquote >example< /blockquote > text.

Sincerely,
Chris

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!