Page 1 of 1

Parsing a String

Posted: Fri Mar 26, 2004 8:54 pm
by Zorth
Does anyone know how I could parse out a string?

Let's say I had the string "The best player is Kanag and his level is 1."

Now let's say I get this string from a file and want to only print the name of the person, in this case it's Kanag. What all would I need to do to get the name alone? Seeing as PHP has no specific types, I haven't really seen a way to run through a string and change it or even compare them.

Any help is appreciated.

Zorth.

Posted: Fri Mar 26, 2004 8:57 pm
by markl999
If the palyer name is always the 5th part of the string, like The best player is fred, The best player is john etc..then you could do..

$parts = explode(' ', $string);
$playername = $parts[4];

Posted: Sat Mar 27, 2004 7:48 am
by Zorth
That answeres scenerio one ;). What if it was like this:
~Kanag More info *Extra info

Basically in the file that is for the program to know what to expect, but is there a way to get rid of the '~' in PHP?

Posted: Sat Mar 27, 2004 8:38 am
by Illusionist
[php_man]str_replace[/php_man]

Code: Select all

$string = "~Kanag More info *Extra info";
$string = str_replace("~","",$string);

Posted: Sat Mar 27, 2004 4:22 pm
by Zorth
Thanks ;).

Posted: Mon Jun 14, 2004 9:37 am
by Heavy
now that you have had some time to play with this, maybe it is time to try regular expressions?

http://www.php.net/manual/en/pcre.pattern.syntax.php

Then you get some real power at your fingertips.
Here's an example:
viewtopic.php?p=113982#113982