Advanced Search

Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Getting part of a string

  1. #1
    Join Date
    May 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting part of a string

    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.

  2. #2
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    PHP Code:
    $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.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  3. #3
    Join Date
    May 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I understand this
    The only problem is
    $name.
    I will NOT know what is inbertween the <spann.. and </span>

  4. #4
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    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.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  5. #5
    Join Date
    May 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by alexjewell View Post
    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?

  6. #6
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    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?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  7. #7
    Join Date
    May 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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> ?

  8. #8
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Alright, well you'd need to know the code immediately before the line. Then, you would do this:

    PHP Code:
    // 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?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  9. #9
    Join Date
    May 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes the spans have different ID'S!
    Thanks very much i shall try this out now
    THANKED.

  10. #10
    Join Date
    May 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ahh
    it worked.
    Thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •