473,320 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Search String in another String using PHP

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.
Nov 10 '08 #1
7 7390
Dormilich
8,658 Expert Mod 8TB
this seems like a job for regular expressions. see preg_match_all() (and related functions)

regards

tutorial on regular expressions: www.regular-expressions.info
Nov 10 '08 #2
Markus
6,050 Expert 4TB
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.
Nov 10 '08 #3
nathj
938 Expert 512MB
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
Nov 10 '08 #4
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.


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
Nov 11 '08 #5
nathj
938 Expert 512MB
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
Nov 11 '08 #6
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.
Nov 12 '08 #7
nathj
938 Expert 512MB
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
Nov 13 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: disaia | last post by:
2 problems: Example: If a person types in a part number into Yahoo: 1. Is there a way for Yahoo to list your web site as one of the results. 2. If the user clicks on your link, can your web...
4
by: Axel | last post by:
Is it possible to write a Stored Procedure that takes a string of search keywords as argument and returns the recordset? At the mo I am passing the WHERE String as argument. I got this...
22
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able search baker search charlie " into this: " able...
3
by: harhaiko | last post by:
Hello, I have 4,4 million rows of data in one column. I want to search for a string in each line. There are random amount of characters before the string I want to find. The string starts with the...
9
by: Christopher Koh | last post by:
I will make a form which will search the database (just like google interface) that will look/match for the exact name in the records of a given fieldname. Any suggestions on how to make the code?
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
5
by: Martien van Wanrooij | last post by:
I have been using phpdig in some websites but now I stored a lot of larger texts into a mysql database. In the phpdig search engine, when you entered a search word, the page where the search word...
1
by: Eric | last post by:
Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly...
5
by: kanley | last post by:
I have a main table with a text description field. In this field, its populated with a string of data. I need to identify from this string of data the name of the vendor using some keywords. I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.