parse a name

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

parse a name

Post by SidewinderX »

ive read the tutorial and done a few searches...and i know there is a thread right below this that askes basically the same thing but ill create a new one because i dont like hijacking other peoples post...

assume i have a string

Code: Select all

$string = 'Down Ozymandias. Rank: 1-Star General ( 14 ) PCID: A13-3E71AC Player Created: June 9, 2006 TABLE.statsx ';
Ozymandias. is the player name which can be 16 aplhanumeric characters including spaces periods and hyphens, what i would like to do is parse the player name, in this case Ozymandias. I tried this, but it dosnt seem to work

Code: Select all

$string = 'Down Ozymandias. Rank: 1-Star General ( 14 ) PCID: A13-3E71AC Player Created: June 9, 2006 TABLE.statsx ';
preg_match('Down (.*?) Rank', $string, $matches);
$str = $matches[1];
echo $matches;
i would appreciate if someone could tell me what is wrong, thank you.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

take a close look at what you are echoing. ;)
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

Code: Select all

$string = 'Down Ozymandias. Rank: 1-Star General ( 14 ) PCID: A13-3E71AC Player Created: June 9, 2006 TABLE.statsx ';
preg_match('Down (.*?) Rank', $string, $matches);
$str = $matches[1];
echo $str;
dosnt work either
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The pattern is missing delimiters.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

ty feyd, as frusterating as your 5 word posts can be.................................

they have probabley helped me learn the most by having to work :evil: for the solution

end result:

Code: Select all

<?php
$string = 'Down Ozymandias. Rank: 1-Star General ( 14 ) PCID: A13-3E71AC Player Created: June 9, 2006 TABLE.statsx ';
preg_match('#Down (.*?) Rank#', $string, $matches);
$str = $matches[1];
echo $str;
?>
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

i thought i was done, but im not :roll:
i incorperated the regex into a little larger code and and it didnt work...go figure

Code: Select all

<?php

$control = "blah blah... Down Ozymandias. Rank: 1-Star General ( 14 ) PCID: A13-3E71AC Player Created: June 9, 2006 TABLE.statsx ...blah blah";
$url = "https://www.novaworld.com/Players/Stats.aspx?id=33680801261&p=616065";
$content = file_get_contents($url); 
$strip = strip_tags($content);
preg_match('#Down (.*?) Rank#', $content, $matches);
$player_name1 = $matches[1];
echo "Player Name: " . $player_name1 . "<br>";

preg_match('#Down (.*?) Rank#', $strip, $matches);
$player_name2 = $matches[1];
echo "Player Name: " . $player_name2 . "<br>";

preg_match('#Down (.*?) Rank#', $control, $matches);
$player_name3 = $matches[1];
echo "Player Name: " . $player_name3 . "<br>";

?>
this gets the contents of the url, strips the html, and just displays all the words on the page. i assumed if i used $strip as the string to parse it would work because, aside from a bunch more data, when i echo $strip and $control they are pratically the same, but in the code above only the regex that works is the one using the controle string. any thoughts?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If "Down" and "Rank" are on separate lines, you will likely need the s modifier for your pattern.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

i have no idea how to do that, ive tried every possable combination of s i could think of....in side the parenthsis, outside, with a forward slash, with a back slash, with no slash, in brackets...to no avail
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have a read through the stickies in this board. We've talked about the various modifiers in them with a fair amount of detail.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: parse a name

Post by SidewinderX »

SidewinderX wrote:ive read the tutorial and done a few searches...
ill take another look, but it still probabley wont help
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

hint: after the pattern delims.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

this works, but i guess its not the 'proper' way to do it

Code: Select all

<?php
$string = 'Down 
Ozymandias. 
Rank: 1-Star General ( 14 ) PCID: A13-3E71AC Player Created: June 9, 2006 TABLE.statsx ';
preg_match('#Down 
(.*?) 
Rank#', $string, $matches);
$str = $matches[1];
echo $str;
?>
EDIT: even so...it dosnt work integrated into a larger script...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If it works this way in this small snippet, it will work exactly the same in a smaller or larger script. The size of the script it is in will have no effect on it. What has an effect on it are the patter modifiers and the subject you are regex searching against.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

Everah wrote:If it works this way in this small snippet, it will work exactly the same in a smaller or larger script. The size of the script it is in will have no effect on it. What has an effect on it are the patter modifiers and the subject you are regex searching against.
thats exactley the problem, i know the regex works fine, it just has to be the content of the string.

When the content is passed through these functions

$content = file_get_contents($url);
$strip = strip_tags($content);

and then echoed, the output (echo $strip;) must be different then the actual content ($strip), no?

like $strip might equal "Down<br>Ozymandias.<br>Rank" but when it is outputed the html is not displayed or something.....
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Maybe try echoing out the var you are matching to see how it changes throughout the process... See if that does anything.
Post Reply