Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 34
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts

    string manipulation help please.

    Hi all,

    i have searched google and looked at the string manual but i think it more complicated than i am able to understand

    i have several records
    e.g

    #1: State Of Play : Complete BBC Series 1 [2003] [DVD]
    #2: Cranford : Complete BBC Series [2007] [DVD]
    #3: Twilight - 2 Disc Special Edition (DVD) (2008)

    and so on which are being pulled from my db but i would like to know how to display it without the "#1: " and if possible the info between the []'s and () as well if there present

    i thought maybe trim would work but looking at the examples in the manual it looks like it may not be as straight forward.

    any ideas

    here is the code i am using
    PHP Code:
    while ($row mysql_fetch_array($item_query)){
                  
    //$title = get_string_between($row['title'], ':','[');
                
    print "<div class='item'>
                
                <div class='itemIMG'>
                <img src='{$row['image']}' 
                alt='{$row['title']}' 
                title='{$row['title']}'
                height='100' />
                </div>
                
                <div class='itemTitle'>{$row['title']}</div>
                <div class='itemInfo'></div>
                <div class='itemButton'>
                <img src='images/compare.gif' 
                alt='Compare {$row['title']} Prices' 
                title=''Compare {$row['title']} Prices''></div>
                
                </div>"

    the $row['title'] is the row the data is stored in which i want to cut down

    any ideas
    cheers

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok i have used

    list ($no, $short_title) = split (': ', $row['title']);

    to remove the "#1: "

    but how do i remove the ['s and ('s after the title???

  • #3
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Regexp is the way to go.

    PHP Code:
    preg_replace("/#\d:\s*|\s*\(.*?\)|\s*\[.*?\]/"""$row['title']) 
    Last edited by venegal; 05-07-2009 at 01:06 AM.

  • Users who have thanked venegal for this post:

    LJackson (05-07-2009)

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    wow thats so cool, i really must learn preg_replace

    thank you!!!!
    Luke

  • #5
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    you can strip out what you don't want with a regex

    Code:
    <?php
    $row["title"] = "#1: State Of Play : Complete BBC Series 1 [2003] [DVD]";
    $row["title"]=preg_replace("/^#\d+:\s+|[(\[][^\])]+[\])]/", "", $row["title"]);
    $row["title"]=trim($row["title"]);
    ?>
    You can not say you know how to do something, until you can teach it to someone else.

  • Users who have thanked timgolding for this post:

    LJackson (05-07-2009)

  • #6
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Well you should learn your regexps fast! Otherwise there is no chance you can take mine and timgolding's, which both have their respective deficiencies, and merge them into something that works robustly.

    For example, because I took only the three strings that you posted into account, mine will stupidly be broken by #10: State Of Play : Complete BBC Series 1 [2003] [DVD]. timgolding's will break if something like #1: State Of Play : Complete BBC Series 1 [2003] [DVD(Multi5)] comes along.

  • #7
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Yeah i noticed those too. But don't have time to fix them now. I reckon the poster should start here http://uk.php.net/manual/en/book.pcre.php
    You can not say you know how to do something, until you can teach it to someone else.

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    cheers i will read up on that,

    luckly i only have #1:,#2:. and #3: so you code should always work for me but i still want to learn it and i will read that link...

    thanks
    Luke

  • #9
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hi guys,

    ok i have a similar request

    im pulling some data out of a feed which contains some htlm code [B] etc which i dont want to display and ~

    e.g it might be
    <b>DVD</b> ~ Daniel Craig

    and if possible i would like it to say
    DVD - Daniel Craig

    is this possible
    thanks
    Luke

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok i have got rid of the <b>'s etc by using strip_tags

    now i am left with

    either
    DVD ~ Dev Patel
    or
    ~ Green Day (Artist)

    how do i remove everything before and including ~ and (artist)

    any ideas
    thanks
    Luke
    Last edited by LJackson; 05-11-2009 at 03:33 PM.

  • #11
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok i have managed to achieve this by going the long way around

    PHP Code:
                $otherinfo strip_tags($row['other']);
                
    $otherinfo trim($otherinfo"DVD");
                
    $otherinfo trim($otherinfo"~ ");
                
    $otherinfo trim($otherinfo"(Artist)");
                
    $otherinfo trim($otherinfo"(Author)"); 
    but at least it works

    cheers

  • #12
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by venegal View Post
    Well you should learn your regexps fast!
    This reminds me of nice sig here "Teach me how to be an engineer, i don't care if it takes all day"

  • #13
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    correction the above doesnt work as it removes all (Artist) as individual characters instead of a sting

    anyone have any ideas?

  • #14
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    p.s does anyone have a good simple to follow regex tutorial to help me learn i have found a few which instantly give me a headache lol need something simple so even a biff can follow

    also i still need to convert these
    DVD ~ Nicole Kidman => Nicole Kidman
    ~ Green Day (Artist) => Green Day
    by Michael Connelly (Author) => by Michael Connelly

    i've tried experimenting but no success, any one have any ideas?

    thanks

  • #15
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    As you have already found out, your "trim" approach does not work, because it removes characters, not strings. You could try something like

    preg_replace("/DVD|~ |\(Artist\)|\(Author\)/", "", $otherinfo);

    And while funny, I don't think the comparison of learning regexps to becoming an engineer holds much truth. It is honestly beyond me how there are whole books written about the topic. Well thinking about it, there are books written about HTML, so what do you know.

  • Users who have thanked venegal for this post:

    LJackson (05-11-2009)


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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