note 41605 added to function.preg-match-all

From: Date: Fri, 16 Apr 2004 11:24:01 +0000
Subject: note 41605 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-68198@lists.php.net to get a copy of this message
<?php /* TagWords And TagString Functions Translated and upgraded versions of Ingpancho's BuscarResaltar and AgregarTagString functions. Author: Noah Slater DOCUMENTATION: Here a improved and translated versions of Ingpancho's previous functions. The original functions were very good, but hard to understand due to the use of spanish for function names and variables. The base function TagString takes a string and searches for a token. If this token is found, it is marked up using the required HTML start and end tags. It will not markup a token if is found to exist within a HTML tag. The function TagString takes four aguments: string [$string] The text you want to search against. string [$token] The token you want to search with. string [$startTag] The HTMl tag you want to prefix every occurence of token. string [$endTag] The HTMl tag you want to suffix every occurence of token. The meta function TagWords takes a number of tokens and used the TagString function to search the same string for them all. The function TagWords takes four aguments: string [$string] The text you want to search against. string [$tokenArray] The list tokens you want to search with. string [$startTag] The HTMl tag you want to prefix every occurence of token. string [$endTag] The HTMl tag you want to suffix every occurence of token. DEMONSTRATION: PHP CODE: $string = 'Hello my name is <a href="mailto:noah@example.com" title="Email Noah!">Noah</a>.' $taggedString = TagWords($text, array('noah'), '<b>', '</b>'); HTML OUTPUT: Hello my name is <a href="mailto:noah@example.com" title="Email Noah!"><b>Noah</b></a>. */ function TagWords($string = '', $tokenArray = array(), $startTag = "<b>", $endTag = "</b>") { // Remove duplicate tokens $tokenArray = array_unique($tokenArray); // Process each token foreach ($tokenArray as $key => $token) { $token = trim($token); $string = TagString($string, $token, $startTag, $endTag); } // Return modified string return $string; } function TagString($string = '', $token = '', $startTag = "<b>", $endTag = "</b>") { if (strlen($token) > 0) { // Count tokens $tokenCount = preg_match_all("/".$token."/i", $string, $taggedTokens); if ($tokenCount>0) { // Fragment string arround tokens $stringFragments = spliti($token,$string); // Begin tagged string variable $taggedString = $stringFragments[0]; // Loop through tokens for ($i = 0; $i < $tokenCount; $i++) { // Init replace flag $replaceFlag = true; // Get the position of the start of the last HTML tag $lastTagStart = strrpos($taggedString, "<"); if (is_int($lastTagStart)) { // Unflag replace, token could be inside a HTML tag $replaceFlag = false; // Get the position of the end of the last HTML tag $lastTagEnd = strrpos($taggedString,">"); if(is_int($lastTagEnd) AND $lastTagEnd > $lastTagStart) { // Flag replace, last tag ended before begining of token $replaceFlag=true; } if($replaceFlag == true) { // Tag token $taggedString .= $startTag.$taggedTokens[0][$i]; $taggedString .= $endTag.$stringFragments[$i+1]; } else { // Do not tag token $taggedString .= $taggedTokens[0][$i]; $taggedString .= $stringFragments[$i+1]; } } } // Return tagged string return stripslashes($taggedString); } else { // Return original string return $string; } } else { // Return original string return $string; } } ?> ---- Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+41605 Delete -- http://master.php.net/manage/user-notes.php?action=delete+41605&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+41605&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#68198) next »