Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>try this </p> <pre><code> function findword($text,array $List){ foreach($List as $Val) $pattern['%([^\da-zA-Z]+)'.$Val.'([^\da-zA-Z]+)%si'] = '&lt;span class="country"&gt;'.$Val.'&lt;/span&gt;'; $text = preg_replace(array_keys($pattern), array_values($pattern), ' '.$text.' '); return $text; } echo findword('Canada is a cold place',array('Canada')); </code></pre> <p>output:</p> <pre><code>&lt;span class="country"&gt;Canada&lt;/span&gt;is a cold place </code></pre> <p><strong>Edit:</strong> if you want replace all match word in text you can use this</p> <pre><code> function findword($text,array $List){ foreach($List as $Val) $pattern['~'.$Val.'~si'] = '&lt;span class="country"&gt;'.$Val.'&lt;/span&gt;'; $text = preg_replace(array_keys($pattern), array_values($pattern), ' '.$text.' '); return $text; } echo findword('Canadaisacold place',array('Canada')); </code></pre> <p>output:</p> <pre><code>&lt;span class="country"&gt;Canada&lt;/span&gt;isacold place </code></pre> <p>Edit2: i wrote it by DOMDocument That Work Good in Html </p> <pre><code> class XmlRead{ static function Clean($html){ $html=preg_replace_callback("~&lt;script(.*?)&gt;(.*?)&lt;/script&gt;~si",function($m){ //print_r($m); // $m[2]=preg_replace("/\/\*(.*?)\*\/|[\t\r\n]/s"," ", " ".$m[2]." "); $m[2]=preg_replace("~//(.*?)\n~si"," ", " ".$m[2]." "); //echo $m[2]; return "&lt;script ".$m[1]."&gt;".$m[2]."&lt;/script&gt;"; }, $html); $search = array( "/\/\*(.*?)\*\/|[\t\r\n]/s" =&gt; "", "/ +\{ +|\{ +| +\{/" =&gt; "{", "/ +\} +|\} +| +\}/" =&gt; "}", "/ +: +|: +| +:/" =&gt; ":", "/ +; +|; +| +;/" =&gt; ";", "/ +, +|, +| +,/" =&gt; "," ); $html = preg_replace(array_keys($search), array_values($search), $html); preg_match_all('!(&lt;(?:code|pre|script).*&gt;[^&lt;]+&lt;/(?:code|pre|script)&gt;)!',$html,$pre); $html = preg_replace('!&lt;(?:code|pre).*&gt;[^&lt;]+&lt;/(?:code|pre)&gt;!', '#pre#', $html); $html = preg_replace('#&lt;!–[^\[].+–&gt;#', '', $html); $html = preg_replace('/[\r\n\t]+/', ' ', $html); $html = preg_replace('/&gt;[\s]+&lt;/', '&gt;&lt;', $html); $html = preg_replace('/\s+/', ' ', $html); if (!empty($pre[0])) { foreach ($pre[0] as $tag) { $html = preg_replace('!#pre#!', $tag, $html,1); } } return($html); } function loadNprepare($content,$encod='') { $content=self::Clean($content); //$content=html_entity_decode(html_entity_decode($content)); // $content=htmlspecialchars_decode($content,ENT_HTML5); $DataPage=''; if(preg_match('~&lt;body(.*?)&gt;(.*?)&lt;/body&gt;~si',$content,$M)){ $DataPage=$M[2]; }else{ $DataPage =$content; } $HTML=$DataPage; $HTML="&lt;!doctype html&gt;&lt;html&gt;&lt;head&gt;&lt;meta charset=\"utf-8\"&gt;&lt;title&gt;Untitled Document&lt;/title&gt;&lt;/head&gt;&lt;body&gt;".$HTML."&lt;/body&gt;&lt;/html&gt;"; $dom= new DOMDocument; $HTML = str_replace("&amp;", "&amp;amp;", $HTML); // disguise &amp;s going IN to loadXML() // $dom-&gt;substituteEntities = true; // collapse &amp;s going OUT to transformToXML() $dom-&gt;recover = TRUE; @$dom-&gt;loadHTML('&lt;?xml encoding="UTF-8"&gt;' .$HTML); // dirty fix foreach ($dom-&gt;childNodes as $item) if ($item-&gt;nodeType == XML_PI_NODE) $dom-&gt;removeChild($item); // remove hack $dom-&gt;encoding = 'UTF-8'; // insert proper return $dom; } function GetBYClass($Doc,$ClassName){ $finder = new DomXPath($Doc); return($finder-&gt;query("//*[contains(@class, '$ClassName')]")); } function findword($text,array $List){ foreach($List as $Val) $pattern['%(\#)?([^\da-zA-Z]+)'.$Val.'([^\da-zA-Z]+)%si'] = '&lt;span class="country"&gt;'.$Val.'&lt;/span&gt;'; $text = preg_replace(array_keys($pattern), array_values($pattern), ' '.$text.' '); return $text; } function FindAndReplace($node,array $List) { if($node==NULL)return false; if (XML_TEXT_NODE === $node-&gt;nodeType || XML_CDATA_SECTION_NODE === $node-&gt;nodeType) { $node-&gt;nodeValue=$this-&gt;findword($node-&gt;nodeValue,$List); return; }else{ if(is_object($node-&gt;childNodes) or is_array($node-&gt;childNodes)) { foreach($node-&gt;childNodes as $childNode) { $this-&gt;FindAndReplace($childNode,$List); } } } } function DOMinnerHTML($element) { $innerHTML = ""; $children = $element-&gt;childNodes; foreach ($children as $child) { $tmp_dom = new DOMDocument(); $tmp_dom-&gt;appendChild($tmp_dom-&gt;importNode($child, true)); $innerHTML.=trim($tmp_dom-&gt;saveHTML()); } $innerHTML=html_entity_decode(html_entity_decode($innerHTML)); return $innerHTML; } function DOMRemove(DOMNode $from) { $from-&gt;parentNode-&gt;removeChild($from); } } $XmlRead=new XmlRead(); $Doc=$XmlRead-&gt;loadNprepare('&lt;a href="?Canada"&gt;Canada&lt;/a&gt; is a cold place'); $XmlRead-&gt;FindAndReplace($Doc,array('Canada')); $Body=$Doc-&gt;getElementsByTagName('body')-&gt;item(0); echo $XmlRead-&gt;DOMinnerHTML($Body); </code></pre> <p>output</p> <pre><code>&lt;a href="?Canada"&gt;&lt;span class="country"&gt;Canada&lt;/span&gt;&lt;/a&gt;is a cold place </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. This table or related slice is empty.
    1. 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