View Full Version : Getting part of a string
MrSheen
09-03-2007, 02:11 PM
Okay guys!
Im wondering how to get a part of a string.
Basically im going to be viewing a part of a profile on a website
and there is this tag:
<span class="nametext"></span>
Now there will be a name in that how would i get what is inbertween?
for example
<span class="nametext">Josh</span>
How would i make it get just the josh?
Thanks.
alexjewell
09-03-2007, 02:40 PM
$name = '<span class="nametext">Josh</span>';
$namePt2 = explode('class="nametext">',$name);
$namePt3 = explode('</span>',$namePt2[1]);
$name = $namePt3[0];
Would output Josh.
If you want any of it explained, just ask.
MrSheen
09-03-2007, 02:42 PM
I understand this
The only problem is
$name.
I will NOT know what is inbertween the <spann.. and </span>
alexjewell
09-03-2007, 02:44 PM
Ok, then what you would want to do is explode the content around the span and set $name to that line - that way whatever is between the span tags doesn't matter.
MrSheen
09-03-2007, 02:44 PM
Ok, then what you would want to do is explode the content around the span and set $name to that line - that way whatever is between the span tags doesn't matter.
Could you create an example for me?
alexjewell
09-03-2007, 02:47 PM
Ok, I'll need more information though.
How/where are you getting this line from? Is it in an html file? And what does the code around it look like?
MrSheen
09-03-2007, 02:52 PM
Yes it has A LOT of html before it and after too.
Is there a way of like saying.. find <span.. then get the string until has found </span> ?
alexjewell
09-03-2007, 03:01 PM
Alright, well you'd need to know the code immediately before the line. Then, you would do this:
// say we're getting the whole file, something.html that contains the line
$file = @file_get_contents('something.html');
$namePt1 = explode('<span class="nametext">',$file);
$namePt2 = explode('</span>',$namePt1[1]);
$name = $namePt2[0];
So when you echo $name, it displays whatever is between the span tags. There's only one instance of this in the whole file right?
MrSheen
09-03-2007, 03:05 PM
Yes the spans have different ID'S!
Thanks very much i shall try this out now :)
THANKED.
MrSheen
09-03-2007, 03:12 PM
ahh
it worked.
Thanks
MrSheen
09-07-2007, 02:52 PM
Is they a faster way of doing this because it takes a while for the script to load.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.