PDA

View Full Version : PHP Help



Trigs
01-02-2009, 10:12 PM
Okay. So lets say theres a website called www.lol.com/index.html (http://www.lol.com/index.html) and on that website it says:

Hi. My name is John Doe.

I use file_get_contents('www.lol.com/index.html'); and specify $lol = file_get_contents('www.lol.com/index.html');

So now $lol is basically: Hi. My name is John Doe.

Now I want to get rid of the Hi. My name is and the . (period) at the end so all I have left is John Doe

How do I do that? Do I use strstr and strrchr? If I do, how do I get the info in between?

Protege
01-02-2009, 10:35 PM
bruv man

explode function & str_replace function bruv

<?php
$lol = 'Hi. My name is John Doe.'
$str = explode( ' ', $lol );
$newString = $str[ 3 ] . ' ' . str_replace( '.', '', $str[ 4 ] );
echo $newString;
?>

aint tested btw

Trigs
01-02-2009, 10:43 PM
Ah yes. I forgot about explode()

Thanks and +rep

Edit: kk all i got was is john

and btw you forgot the ; on line 2

HabbDance
02-02-2009, 12:14 AM
bruv man

explode function & str_replace function bruv

<?php
$lol = 'Hi. My name is John Doe.'
$str = explode( ' ', $lol );
$newString = $str[ 3 ] . ' ' . str_replace( '.', '', $str[ 4 ] );
echo $newString;
?>

aint tested btw


Ah yes. I forgot about explode()

Thanks and +rep

Edit: kk all i got was is john

and btw you forgot the ; on line 2
Ik close to nothing about PHP, but just try fiddling with the code, for example maybe change the numbers? (No idea what that will do, but you can always change back) Good luck :)

Agnostic Bear
02-02-2009, 12:35 AM
Okay. So lets say theres a website called www.lol.com/index.html (http://www.lol.com/index.html) and on that website it says:

Hi. My name is John Doe.

I use file_get_contents('www.lol.com/index.html'); and specify $lol = file_get_contents('www.lol.com/index.html');

So now $lol is basically: Hi. My name is John Doe.

Now I want to get rid of the Hi. My name is and the . (period) at the end so all I have left is John Doe

How do I do that? Do I use strstr and strrchr? If I do, how do I get the info in between?

assuming it will always be a 2 word name, use this:



<?php
$file = file_get_contents( 'filename' );
$file = explode( ' ', $file );
$c = count( $file ) - 1;
$name = $file[ ( $c - 1 ) ] . ' ' . $file[ $c ];
$name = preg_replace( '#\.$#', '', $name );
then use name

i am not sure but this should work

edit: it does work i just tried it

Trigs
02-02-2009, 12:58 AM
I don't understand one thing from what jewish bear posted...

Okay well what I want to do is get Chris Paul's PPG, APG, and RPG from http://sports.yahoo.com/nba/players/3930

Moh
02-02-2009, 01:21 AM
This code will steal the charts from Radio 1's Charts:



$page = file_get_contents("http://www.bbc.co.uk/radio1/chart/singles.shtml");
$start = explode('<!-- CHART TABLE -->', $page, 2);
$end = explode('<!-- END CHART TABLE -->', $start[1], 2);
$result= trim($end[0]);
echo $result;


If you look in the source of the charts page, you will see they have the following lines:
<!-- CHART TABLE -->
<!-- END CHART TABLE -->

That code will get anything in between them :)

HotelUser
02-02-2009, 02:11 AM
I don't understand one thing from what jewish bear posted...


If the text "Hi. My name is" never changes use Protege's. If you think the "Hi. My name is" might ever change, use Jewish Bear's.

If it was me I would have just done this:


<?php
$lol = file_get_contents('www.lol.com/index.html');
$split = explode('Hi. My name is ',$lol);
$split = explode('.',$split[1]);
$name = $split[0];

echo $name;

?>I'd do it that way incase there was any additional text or formatting on the page you're getting the source from.

Want to hide these adverts? Register an account for free!