Forum Moderators: coopster

Message Too Old, No Replies

Get string between two characters

Get string between two characters php help

         

ephex

7:23 am on Dec 24, 2003 (gmt 0)

10+ Year Member



I will try to explain this in the most simple way possible.

I need a function of some sort to get the string between [1] and [2] in the following example.

$text = "[1]how are you[2] today?";

and make the output "how are you". I am sure this is possible, and I need a reply ASAP, thanx.

ephex

7:51 am on Dec 24, 2003 (gmt 0)

10+ Year Member



Also, how do I read from a certian line. example below:

Line1: how are you
Line2: doing today?
Line3: good, thank you

and i want it to be able to read line 2, so it would print "doing today?". possible?

coopster

2:02 pm on Dec 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, ephex!

There are a number of ways to get the results you want. You could use a combination of PHP's String Functions [php.net], or POSIX-extended regex functions [php.net], but I would elect one of the PCRE [php.net] (Perl Compatible Regular Expression) functions:


# Get data between any numeric bracket markers:
$text = '[1]how are you[2] today?';
$value = preg_replace [php.net]("/.*\[\d+](.*)\[\d+].*/", "$1", $text, 1);

OR

# Get data between any numeric bracket markers:
$text = '[1]how are you[2] today?';
preg_match [php.net]("/\[\d+](.*)\[\d+]/", $text, $matches);
$value = $matches[1];

Analyze these by referring to the PHP Manual links and you'll be off and running with Regular Expressions. There are a number of good resources regarding Regular Expressions on the web, including a primer in our PHP Forum Library [webmasterworld.com].

ephex

7:37 pm on Dec 24, 2003 (gmt 0)

10+ Year Member



What about reading whole strings from a line?

coopster

2:33 pm on Dec 26, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Where are you reading the strings from? A text file?