# [web] String extraction in php

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

## 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

1. 1
2. 2
3. 3
4. 4
frob
14
5. 5

• 16
• 12
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632167
• Total Posts
3004527

×