Advertisement
Lucassim

regexps in PHP

Jan 5th, 2013
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.68 KB | None | 0 0
  1. /*
  2.  *
  3.  *  Searchs de string for latex code (between $$) and returns link with the corresponding image if found
  4.  *
  5.  */
  6.  
  7.     function replaceLatex($text){
  8.        
  9.         $match = preg_match_all('(\$[^\$]*\$)',$text,$latexTextArray);
  10.         //if there isnt latex in the text
  11.         if ($match==0) return $text;
  12.        
  13.        
  14.         $replacedText=$text;
  15.         foreach ($latexTextArray[0] as &$latexText){
  16.             //Remove the $ signs
  17.             $result = substr($latexText,1);
  18.             $result = substr($result,0,-1);
  19.            
  20.             //We replace the text for the latex img
  21.             $replacedText = str_replace($latexText, "<img src=\"http://latex.codecogs.com/gif.latex?".$result."\" border=\"0\"/> ", $replacedText);
  22.        
  23.         }
  24.         return $replacedText;
  25.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement