SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seem to have a mental block (or perhaps it's 4am). Can't get my head round regular expressions. Whether it's because I'm mixing the concept of wildcard characters with regular expressions, I dont know.

    Can anyone recommend a online tutorial that won't assume I too smart or too stupid?


    [Slyly hope someone will solve it for him...not really]
    Basically trying to convert a character string into a table;

    <?php

    $text = ( "This is a table HeadingA HeadingB 234 534 HeadingC HeadingD 42% 95%" );

    $text = ereg_replace ( huh? , huh?, $text );

    So that it end ups something like;

    $text = ( "
    <p>This is a table</p>
    <table>
    <tr>
    <td>HeadingA</td><td>HeadingB</td>
    </tr>
    <tr>
    <td>234</td><td>534</td>
    </tr>
    <tr>
    <td>HeadingC</td><td>HeadingD</td>
    </tr>
    <tr>
    <td>42%</td><td>95%</td>
    </tr>
    </table> " )

    Are regular expressions actually the right way to go with a situation like this?

    Anyway, many thanks and goodnight.

  2. #2
    SitePoint Evangelist
    Join Date
    May 2000
    Location
    Canada
    Posts
    533
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    regex might be something to look into, but in THIS SPECIFIC CASE, since i dont want to think regex right now (just woke up a while ago), i'd put the data you need with a specific delimiter, and parse, like this:

    $text = "This is a table::HeadingA::HeadingB::234::534::HeadingC::HeadingD::42%::95%" );

    $asdf = explode("::",$text);

    $text = "
    <p>".$text[0]."</p>
    <table>
    <tr>
    <td>".$text[1]."</td><td>".$text[2]."</td>
    </tr>
    <tr>
    <td>".$text[3]."</td><td>".$text[4]."</td>
    </tr>
    <tr>
    <td>".$text[5]."</td><td>".$text[6]."</td>
    </tr>
    <tr>
    <td>".$text[7]."</td><td>".$text[8]."</td>
    </tr>
    </table> ";

    Regards,
    Vinay Sahni
    SitePoint Community Moderator
    myPHPhost.com: mySQL 3.23, php4.0.3pl1, optimizer

  3. #3
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Vinay...where does the $asdf come into play? If it is not used, could you just leave it out and say explode("::",$text);
    ?
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    NO I think vinay forgot to use $asdf[0] or [1] for his newly formed array.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so instead of this:

    <td>".$text[1]."</td><td>".$text[2]."</td>

    it should be this:

    <td>".$asdf[1]."</td><td>".$asdf[2]."</td>

    ?
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes you are taking the string $text and splitting on the double colon and placing each part into an array named $asdf. So you would be accessing elements off the $asdf array not the $text array.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow...explode sounds like a pretty cool function...is it used oftern?
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Most defintely you can use it for splitting a \ny string on any delimiter unless the delimiter is in the string like

    $str = "This,That,ThisandThat,Thisor,That";

    And you split it on a comma, your results might not come out right

    I usually use || as a delimiter. The most common reason why I use this is to store an array in a database. Before it goes in I implode the array

    $str = implode("||", $arrayname);

    Now $str holds all the elements of the array in one string separated by the delimiter in this case a || so now when I pull the data out of the db I can use
    $arrayname = explode("||", $stringfromdb);

    Now I have an array again
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Phew! - finally got it working - with the explode trick above and saved a major headache. If I ever work out exactly how regular expressions work I'll send you all a copy of the book I'll write about it "The Joy of EReg".
    Many thanks again.

    Was looking at another tutorial today about PHP authentication and tracking which has an example of using explode to chop up a text file containing userid: password info. For anyone else who's learning this PHP business check it out;

    http://hotwired.lycos.com/webmonkey/...ex2a_meta.html


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
  •