FlyingCat
FlyingCat

Reputation: 14250

How to manipulate strings in php

I have a question regarding the DomDocument

In my previous questoin.. How to detect certain characters and wrap them with another string?

I can get the assign table to the string. However, there are some table cell that contains

<input type='text' value='input value'/>

so it's like

<td><input type='text' value='input value'/></td>

I want to remove the input tag but still display the 'input value' in my cell as there is no input box. I need it because I need to display my string in my email.

I can't really do it in the client side.

Is there anyway to do this? Thanks.

Upvotes: 0

Views: 65

Answers (1)

Amal Murali
Amal Murali

Reputation: 76646

You can extract the input value using DomDocument and an appropriate XPath:

$html = "<td><input type='text' value='input value'/></td>";
$dom = new DOMDocument();
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);

$inputtags = $xpath->query('//input[@type="text"]');
foreach ($inputtags as $tag) {
    $value = $tag->getAttribute('value');
}

Output:

input value

Note: The XPath used here is just for demonstration purposes. There may be multiple elements with input type as text and it's probably a good idea to use a more solid XPath. However, this should get you started.

Demo!

Upvotes: 2

Related Questions