3

I want to replace <a> tag contents from PHP Variable ($html_content)...

The Variable $html_content Value looks like below:

$html_content= "<table>
<thead>
<tr><th>Customer</th><th>Age</th><th>A</th></tr>
</thead>
<tbody>
<tr><td>Test 1</td><td>25</td><td><a href='www.google.com'>Link</a></td></tr>
<tr><td>Test 2</td><td>30</td><td><a href='www.yahoo.com'>Link</a></td></tr>
<tr><td>Test 3</td><td>31</td><td><a href='www.rediff.com'>Link</a></td></tr>
...
...
...
</tbody>
</table>

I want to replace content in string every last <td> content of every <tr> which contains <a> tag.

The sample rows result:

<tr><td>Test 1</td><td>25</td><td>Action will be taken soon.</td></tr>
<tr><td>Test 2</td><td>30</td><td>Action taken yesterday</td></tr>
...
...

I need result to replace every row with different content values. The content is taken from table. The particular customer action only i want to replace.

CUSTOMER TABLE:
------------------------------------------------------
id    customer    age    action 
------------------------------------------------------
1     Test 1      25     Action will be taken soon
2     Test 2      30     Action taken yesterday
3     Test 3      31     Action will take tomw
4     Kumar       28     Suspended
...
...
...

------------------------------------------------------

using this table i have shown filtered result in one page without action description instead of <a> tag. (This result is part of that page named reports.php). I want to export these records only with action field description to PDF(I'm using HTML to PDF class). In that page i had sent to these HTML values to that page and storing in PHP Variable($html_content) in export_pdf.php page...

The table records is not fixed size... It's based on generating result from sorting.

Please anyone help me... Thanks in advance...

2 Answers 2

1

Try following codes:

$html_content= preg_replace("/<a[^>]+\>(.*?)<\/a>/i", "Action will be taken soon.", $html_content); 

With foreach in query

foreach($row = mysql_fetch_assoc($result))
{
    $html_content = preg_replace("/<a[^>]+\>(\w+)<\/a>/i", $row['action'], $html_content); 
}

Output

<table>
   <thead>
      <tr><th>Customer</th><th>Age</th><th>A</th></tr>
   </thead>
   <tbody>
      <tr><td>Test 1</td><td>25</td><td>Action will be taken soon.</td></tr>
      <tr><td>Test 2</td><td>30</td><td>Action will be taken soon.</td></tr>
      <tr><td>Test 3</td><td>31</td><td>Action will be taken soon.</td></tr>
   </tbody>
</table>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Bora your Response... But I need result to replace every row with different content values. The content is taken from table. The particular customer action only i want to replace.
Could you give me an example or paste to question? I'll update
Bora :I edited the question... Plz see that... could u understand?
Ok, your replace texts will be, yesterday etc. Texts changes according to what? And where do you store these texts? In array? I have to know.
@Vasu Updated with foreach. You can replace while getting result from query with foreach
1

You can do it via regex:

$rgReplace = ['foo', 'bar', 'baz'];
$i         = 0;
$html_content=preg_replace_callback('/\<td[^\<\>]*\>\<a[^\<\>]*\>[^\<\>]*\<\/a\>\<\/td\>/', 
function($rgMatches) use ($rgReplace, &$i)
{
   return $rgReplace[$i++];
}, $html_content);

(your $rgReplace is an array with desired replaces).

or, alternatively, via DOMDocument API.

1 Comment

Thanks Alma Do Mundo your response. But I need result to replace every row with different content values. The content is taken from table. The particular customer action only i want to replace. I edited the question... plz see that...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.