Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please see my example code below on how to accomplish this.</p> <pre><code>&lt;?php $xml = &lt;&lt;&lt;XML &lt;videos&gt; &lt;video&gt; &lt;id&gt;1&lt;/id&gt; &lt;enSub&gt;Hello Foo! Good morning!&lt;/enSub&gt; &lt;cnSub&gt;你好 Foo! 早上好!&lt;/cnSub&gt; &lt;/video&gt; &lt;video&gt; &lt;id&gt;2&lt;/id&gt; &lt;enSub&gt;Hello Bar! Good afternoon!&lt;/enSub&gt; &lt;cnSub&gt;你好 Bar! 下午好!&lt;/cnSub&gt; &lt;/video&gt; &lt;/videos&gt; XML; // Lowercase the XML so we can do a non-case-sensitive search. $xml = strtolower($xml); // Create a DOMDocument based on the xml. $dom = new DOMDocument; $dom-&gt;loadXML($xml); // Create an xpath based on the dom document so we can search it. $xpath = new DOMXpath($dom); // Search for any video tag that contains the text good morning. $nodes = $xpath-&gt;query('//video[contains(.,\'good morning\')]'); // Iterate all nodes foreach($nodes as $node){ // find the ID node and print its content. var_dump($xpath-&gt;query('id',$node)-&gt;item(0)-&gt;textContent); } </code></pre> <p>-- Edit</p> <p>I reread your post and it looks like you're using keywords and not strings. If that's the case, then try this snippet on for size:</p> <pre><code>&lt;?php $xml = &lt;&lt;&lt;XML &lt;videos&gt; &lt;video&gt; &lt;id&gt;1&lt;/id&gt; &lt;enSub&gt;Hello Foo! Good morning!&lt;/enSub&gt; &lt;cnSub&gt;你好 Foo! 早上好!&lt;/cnSub&gt; &lt;/video&gt; &lt;video&gt; &lt;id&gt;2&lt;/id&gt; &lt;enSub&gt;Hello Bar! Good afternoon!&lt;/enSub&gt; &lt;cnSub&gt;你好 Bar! 下午好!&lt;/cnSub&gt; &lt;/video&gt; &lt;/videos&gt; XML; // Lowercase the XML so we can do a non-case-sensitive search. $xml = strtolower($xml); // Create an DOMDocument based on the xml. $dom = new DOMDocument; $dom-&gt;loadXML($xml); // Create an xpath based on the dom document so we can search it. $xpath = new DOMXpath($dom); // Define the search keywords $searchKeywords = array('good','hello'); // Iterate all of them to make them into valid xpath $searchKeywords = array_map( function($keyword){ // Replace any single quotes with an escaped single quote. $keyword = str_replace('\'','\\\'',$keyword); return 'contains(.,\''.$keyword.'\')'; }, $searchKeywords ); // Implode all the keywords using and, you could change this to be // an"or" condition if you so desire. $searchKeywords = implode(' and ',$searchKeywords); // The search keywords now look like contains(.,'good') and contains(.,'hello') // Search for any video tag that contains the text good morning. $nodes = $xpath-&gt;query('//video['.$searchKeywords.']'); // Iterate all nodes foreach($nodes as $node){ // find the ID node and print its content. var_dump($xpath-&gt;query('id',$node)-&gt;item(0)-&gt;textContent); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload