Reputation: 1222
I have an array of countries the key is the country code, the value is the country name, now i have a string, which is posted by users, i want to find if the string has country in it n replace it with
<span class="country">$1</span>
to make it even clearer : let's say i have this text :
Canada is a cold place
i want it to be :
<span class="country">canada</span> is a cold place
where i use my countries array to find and repalce.
the reason behind this is i want to use the microformats, so i need to extract specific text from a string.
i had similar preg_replaces code
$style = array(
'/\[b\](.*)?\[\/b\]/isU' => '<b>$1</b>',
'/\[i\](.*)?\[\/i\]/isU' => '<i>$1</i>',
'/\[u\](.*)?\[\/u\]/isU' => '<u>$1</u>',
'/\[em\](.*)?\[\/em\]/isU' => '<em>$1</em>',
'/\[li\](.*)?\[\/li\]/isU' => '<li>$1</li>',
'/\[code\](.*)?\[\/code\]/isU' => '<div class="tx_code">$1</div>',
'/\[q\](.*)?\[\/q\]/isU' => '<q>$1</q>',
'/[\r\n]{3}+/' => "\n"
);
$text = preg_replace(array_keys($style),array_values($style),$text);
which works, i need something like that.
Keep in mind, that it should not be case sensitive, some users may post canada or Canada
thanks
Upvotes: 2
Views: 718
Reputation: 1222
i wrote my own, and it was the best so far :
if($microformat){
foreach ($this->countries as $co){
$text = preg_replace('/(\#)?\b'.$co.'\b/isU','<span class="country">$0</span>',$text);
}
}
thank you all
Upvotes: 0
Reputation: 3149
try this
function findword($text,array $List){
foreach($List as $Val)
$pattern['%([^\da-zA-Z]+)'.$Val.'([^\da-zA-Z]+)%si'] = '<span class="country">'.$Val.'</span>';
$text = preg_replace(array_keys($pattern), array_values($pattern), ' '.$text.' ');
return $text;
}
echo findword('Canada is a cold place',array('Canada'));
output:
<span class="country">Canada</span>is a cold place
Edit: if you want replace all match word in text you can use this
function findword($text,array $List){
foreach($List as $Val)
$pattern['~'.$Val.'~si'] = '<span class="country">'.$Val.'</span>';
$text = preg_replace(array_keys($pattern), array_values($pattern), ' '.$text.' ');
return $text;
}
echo findword('Canadaisacold place',array('Canada'));
output:
<span class="country">Canada</span>isacold place
Edit2: i wrote it by DOMDocument That Work Good in Html
class XmlRead{
static function Clean($html){
$html=preg_replace_callback("~<script(.*?)>(.*?)</script>~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 "<script ".$m[1].">".$m[2]."</script>";
}, $html);
$search = array(
"/\/\*(.*?)\*\/|[\t\r\n]/s" => "",
"/ +\{ +|\{ +| +\{/" => "{",
"/ +\} +|\} +| +\}/" => "}",
"/ +: +|: +| +:/" => ":",
"/ +; +|; +| +;/" => ";",
"/ +, +|, +| +,/" => ","
);
$html = preg_replace(array_keys($search), array_values($search), $html);
preg_match_all('!(<(?:code|pre|script).*>[^<]+</(?:code|pre|script)>)!',$html,$pre);
$html = preg_replace('!<(?:code|pre).*>[^<]+</(?:code|pre)>!', '#pre#', $html);
$html = preg_replace('#<!–[^\[].+–>#', '', $html);
$html = preg_replace('/[\r\n\t]+/', ' ', $html);
$html = preg_replace('/>[\s]+</', '><', $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('~<body(.*?)>(.*?)</body>~si',$content,$M)){
$DataPage=$M[2];
}else{
$DataPage =$content;
}
$HTML=$DataPage;
$HTML="<!doctype html><html><head><meta charset=\"utf-8\"><title>Untitled Document</title></head><body>".$HTML."</body></html>";
$dom= new DOMDocument;
$HTML = str_replace("&", "&", $HTML); // disguise &s going IN to loadXML()
// $dom->substituteEntities = true; // collapse &s going OUT to transformToXML()
$dom->recover = TRUE;
@$dom->loadHTML('<?xml encoding="UTF-8">' .$HTML);
// dirty fix
foreach ($dom->childNodes as $item)
if ($item->nodeType == XML_PI_NODE)
$dom->removeChild($item); // remove hack
$dom->encoding = 'UTF-8'; // insert proper
return $dom;
}
function GetBYClass($Doc,$ClassName){
$finder = new DomXPath($Doc);
return($finder->query("//*[contains(@class, '$ClassName')]"));
}
function findword($text,array $List){
foreach($List as $Val)
$pattern['%(\#)?([^\da-zA-Z]+)'.$Val.'([^\da-zA-Z]+)%si'] = '<span class="country">'.$Val.'</span>';
$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->nodeType || XML_CDATA_SECTION_NODE === $node->nodeType) {
$node->nodeValue=$this->findword($node->nodeValue,$List);
return;
}else{
if(is_object($node->childNodes) or is_array($node->childNodes)) {
foreach($node->childNodes as $childNode) {
$this->FindAndReplace($childNode,$List);
}
}
}
}
function DOMinnerHTML($element)
{
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child)
{
$tmp_dom = new DOMDocument();
$tmp_dom->appendChild($tmp_dom->importNode($child, true));
$innerHTML.=trim($tmp_dom->saveHTML());
}
$innerHTML=html_entity_decode(html_entity_decode($innerHTML));
return $innerHTML;
}
function DOMRemove(DOMNode $from) {
$from->parentNode->removeChild($from);
}
}
$XmlRead=new XmlRead();
$Doc=$XmlRead->loadNprepare('<a href="?Canada">Canada</a> is a cold place');
$XmlRead->FindAndReplace($Doc,array('Canada'));
$Body=$Doc->getElementsByTagName('body')->item(0);
echo $XmlRead->DOMinnerHTML($Body);
output
<a href="?Canada"><span class="country">Canada</span></a>is a cold place
Upvotes: 1