Connecting Tech Pros Worldwide Forums | Help | Site Map

Search String in another String using PHP

Newbie
 
Join Date: May 2007
Posts: 5
#1: Nov 10 '08
HI all,
I'm using $content = str_ireplace($search, $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

Expand|Select|Wrap|Line Numbers
  1. <table with="100" height=""><tr><td class="cont" style="hsd">
  2. <a href="test.php">content</a>
  3. </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.

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#2: Nov 10 '08

re: Search String in another String using PHP


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

regards

tutorial on regular expressions: www.regular-expressions.info
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,938
#3: Nov 10 '08

re: Search String in another String using PHP


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.
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 854
#4: Nov 10 '08

re: Search String in another String using PHP


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:

Expand|Select|Wrap|Line Numbers
  1. $var_start ='<table with="100" height=""><tr><td class="cont" style="hsd"><a href="test.php">content</a></td></tr></table>' ;
  2.  
  3. $lnStartPos = strpos($var_start, '<a href') ;
  4. $lnEndPos = strpos($var_start, '</a>') ;
  5.  
  6. $var_end = substr($var_start, $lnStartPos, $lnEndPos) ;
  7. echo $var_end;
  8.  
I hope that helps
nathj
Newbie
 
Join Date: May 2007
Posts: 5
#5: Nov 11 '08

re: Search String in another String using PHP


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.

Expand|Select|Wrap|Line Numbers
  1.  $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>
  2. <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>";
  3.  
  4.  $Start = strpos($var_start, '<a href') ;
  5.    $End = strpos($var_start, '</a>') ;
  6.  
  7.    $var_end = substr($var_start, $Start, $End) ;
  8.    echo $var_end;
  9.  
Really need your advise. Thanks in advance for your time and kind help.

p/s: Noted Markus.
Regards, myza.


Quote:
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:

Expand|Select|Wrap|Line Numbers
  1. $var_start ='<table with="100" height=""><tr><td class="cont" style="hsd"><a href="test.php">content</a></td></tr></table>' ;
  2.  
  3. $lnStartPos = strpos($var_start, '<a href') ;
  4. $lnEndPos = strpos($var_start, '</a>') ;
  5.  
  6. $var_end = substr($var_start, $lnStartPos, $lnEndPos) ;
  7. echo $var_end;
  8.  
I hope that helps
nathj
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 854
#6: Nov 11 '08

re: Search String in another String using PHP


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
Newbie
 
Join Date: May 2007
Posts: 5
#7: Nov 12 '08

re: Search String in another String using PHP


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.
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 854
#8: Nov 13 '08

re: Search String in another String using PHP


Quote:

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
Reply