[web] String extraction in php

Started by
1 comment, last by Bahlor 15 years, 8 months ago
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 = '
Hello
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 . So that it then becomes: $Var = '
Hello
'; $var2 = 'Yes hello there!';
Now is this possible in php, and if so how is it done?
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).

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

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
example
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
Vini,Vidi,Vici (Gaius Iulius Caesar)

This topic is closed to new replies.

Advertisement