Search String in another String using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nurmaiza
    New Member
    • May 2007
    • 5

    Search String in another String using PHP

    HI all,
    I'm using $content = str_ireplace($s earch, $replace, $var3); to search and replace value in $var3.
    My problem is i want to take out all the table attribute found in $var3.
    For example

    Code:
    <table with="100" height=""><tr><td class="cont" style="hsd">
    <a href="test.php">content</a>
    </td></tr></table>
    I only want to echo the link content without the table structure at all.

    Is there any pattern to search all the table attribute and replace with null value?

    I got a long string from database to remove the table structure, very appreciate your kind help. Thanks in advance.

    Regards,myza.
    Last edited by Markus; Nov 10 '08, 11:51 AM. Reason: added # tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    this seems like a job for regular expressions. see preg_match_all( ) (and related functions)

    regards

    tutorial on regular expressions: www.regular-expressions.info

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Hey, Nurmaiza.

      I've added [code] tags to your post to format the code you posted. Please do make a note to do this when you're posting code.

      Thanks.

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #4
        Hi,

        Regular expressions are probably the best way to go with this but here's an alternative using some of the many string manipulation functions built into PHP:

        Code:
        $var_start ='<table with="100" height=""><tr><td class="cont" style="hsd"><a href="test.php">content</a></td></tr></table>' ;
        
        $lnStartPos = strpos($var_start, '<a href') ;
        $lnEndPos = strpos($var_start, '</a>') ;
        
        $var_end = substr($var_start, $lnStartPos, $lnEndPos) ;
        echo $var_end;
        I hope that helps
        nathj

        Comment

        • nurmaiza
          New Member
          • May 2007
          • 5

          #5
          Hi,
          Thank you nathj, I've tried the code and resulting content with link to display. Now the content display partially. I got long string, my intention is to remove the table structure, only echo the wording as well as the link. Someone will post the content everyday, and this program will remove its table format and style.

          Code:
           $var_start ="<table border='0'  cellpadding='0' class='sub' bgcolor='#f0f0f0'> <tbody> <tr> <td colspan='3' valign='top' style='text-align: left'><img src='images/stories/2008aug2/0908bc01-aug9.jpg' border='0' /></td> </tr> <tr style='text-align: left'> <td colspan='3' valign='top' style='text-align: left'> <p>File picture of Zulkifli Nordin (extreme right) at the Bar Council meeting.</p> </td> </tr> <tr style='text-align: left'> <td width='70%' valign='top' style='text-align: left'> <a href='http://localhost/mversion2/articles.php?id=7805'>Zul Nordin resurfaces unrepentant</a></span> <p><span style='font-size: small'>PARLIAMENT, Aug 18 - PKR MP Zulkifli Nordin today emerged from hiding â€- unrepentant and defiant over his role in the fracas at the Bar Councilâ€>s forum on conversion and Islam on August 9. Indeed, he was in a fighting mood and said that he was considering proposing a motion in Parliament to sanction the Bar Council for its anti-Islam stand.</span></p> <p>
          <a href='http://localhost/mversion2/articles.php?id=7805'>Read More </a></p> </td><td  valign='top' style='text-align: left'>&nbsp;</td> <td width='30%' valign='top' style='text-align: left'> <ul> <li> <p><a href='http://localhost/mversion2/articles.php?id=7783'>Zeti: Asia to continue facing inflationary pressure</a></p> </li> </ul> <ul> <li> <p><a class='contentpagetitle' href='http://localhost/mversion2/articles.php?id=7784'> Accuserâ's Quran vow makes it<br />tougher for Anwar</a></p> </li> </ul> <ul> <li> <p><a class='contentpagetitle' href='http://localhost/mversion2/articles.php?id=7793'> Thunderâ rumbles past 
Dark Knightâ with US$26m</a></p> </li> </ul> <ul> <li> <p><a class='contentpagetitle' href='http://localhost/mversion2/articles.php?id=7800'> Commodities flash a signal of <br />global relief</a></p> </li> </ul> </td> </tr> </tbody> </table>";
            
           $Start = strpos($var_start, '<a href') ;
             $End = strpos($var_start, '</a>') ;
              
             $var_end = substr($var_start, $Start, $End) ;
             echo $var_end;
          Really need your advise. Thanks in advance for your time and kind help.

          p/s: Noted Markus.
          Regards, myza.


          Hi,

          Regular expressions are probably the best way to go with this but here's an alternative using some of the many string manipulation functions built into PHP:

          Code:
          $var_start ='<table with="100" height=""><tr><td class="cont" style="hsd"><a href="test.php">content</a></td></tr></table>' ;
          
          $lnStartPos = strpos($var_start, '<a href') ;
          $lnEndPos = strpos($var_start, '</a>') ;
          
          $var_end = substr($var_start, $lnStartPos, $lnEndPos) ;
          echo $var_end;
          I hope that helps
          nathj

          Comment

          • nathj
            Recognized Expert Contributor
            • May 2007
            • 937

            #6
            Ok, you need to get the full <a> tag not just the text in the middle. This shouldn't be too difficult you just need to play around with the value of the start position.

            Try experimenting with that value, adjusting it and seeing what comes out using the code snippet I originally posted. It's also worth reading up on the string manipulation functions within php.

            Have a play and honestly it will come right you just need to mess around with the code sample a bit. By doing this you will also gain a better understanding of how these functions work and what they are doing which means you will be able to use them more effectively in the future and be able to maintain this code more efficiently.

            Cheers
            nathj

            Comment

            • nurmaiza
              New Member
              • May 2007
              • 5

              #7
              I tried to play around...its already 3 days trying the same things.I know its easy but I found table structure/attribute is hard to remove.

              Comment

              • nathj
                Recognized Expert Contributor
                • May 2007
                • 937

                #8
                Originally posted by nurmaiza
                I tried to play around...its already 3 days trying the same things.I know its easy but I found table structure/attribute is hard to remove.
                The original code is getting out the full link. However, when you echo that variable it prints it as a link. So the code I gave you does what you need, I think. What you do with the variable after that is another matter. The variable itself will contain the full <a> tag for you.

                Run the code and view the source to check it out.

                Cheers
                nathj

                PS Sorry about the earlier confusion - I was pretty tired when I replied and not really thinking straight

                Comment

                Working...