[web] String extraction in php

This topic is 3737 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
Rutin
16
4. 4
5. 5

• 14
• 9
• 9
• 9
• 10
• Forum Statistics

• Total Topics
632915
• Total Posts
3009194
• Who's Online (See full list)

There are no registered users currently online

×