Page 1 of 2
parse a name
Posted: Sun Jul 23, 2006 6:08 pm
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.
Posted: Sun Jul 23, 2006 7:27 pm
by feyd
take a close look at what you are echoing.

Posted: Sun Jul 23, 2006 9:17 pm
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
Posted: Sun Jul 23, 2006 10:37 pm
by feyd
The pattern is missing delimiters.
Posted: Sun Jul 23, 2006 10:58 pm
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

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;
?>
Posted: Mon Jul 24, 2006 12:04 am
by SidewinderX
i thought i was done, but im not
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?
Posted: Mon Jul 24, 2006 7:40 am
by feyd
If "Down" and "Rank" are on separate lines, you will likely need the s modifier for your pattern.
Posted: Mon Jul 24, 2006 1:05 pm
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
Posted: Mon Jul 24, 2006 1:07 pm
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.
Re: parse a name
Posted: Mon Jul 24, 2006 1:49 pm
by SidewinderX
SidewinderX wrote:ive read the tutorial and done a few searches...
ill take another look, but it still probabley wont help
Posted: Mon Jul 24, 2006 3:15 pm
by Burrito
hint: after the pattern delims.
Posted: Fri Aug 04, 2006 8:03 pm
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...
Posted: Fri Aug 04, 2006 9:55 pm
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.
Posted: Mon Aug 07, 2006 11:29 pm
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.....
Posted: Tue Aug 08, 2006 1:07 pm
by RobertGonzalez
Maybe try echoing out the var you are matching to see how it changes throughout the process... See if that does anything.