Jooml : preg_replace is deprecated - php

I am modifying one of our Joomla template and I am getting this warning.
Deprecated: preg_replace(): The /e modifier is deprecated, use
preg_replace_callback instead in
/home/folder/public_html/components/com_joomgallery/helpers/helper.php
on line 255
Code is like this :
$text = preg_replace('/('.$replace2.')/ie', $replace, $text);
Entire code block :
public static function createPagetitle($text, $catname = '', $imgtitle = '', $page_title = '')
{
preg_match_all('/(\[\!.*?\!\])/i', $text, $results);
define('COM_JOOMGALLERY_COMMON_CATEGORY', JText::_('COM_JOOMGALLERY_COMMON_CATEGORY'));
define('COM_JOOMGALLERY_COMMON_IMAGE', JText::_('COM_JOOMGALLERY_COMMON_IMAGE'));
for($i = 0; $i<count($results[0]); $i++)
{
$replace = str_replace('[!', '', $results[0][$i]);
$replace = str_replace('!]', '', $replace);
$replace = trim($replace);
$replace2 = str_replace('[!', '\[\!', $results[0][$i]);
$replace2 = str_replace('!]', '\!\]', $replace2);
$text = preg_replace('/('.$replace2.')/ie', $replace, $text);
}
$text = str_replace('#cat', $catname, $text);
$text = str_replace('#img', $imgtitle, $text);
$text = str_replace('#page_title', $page_title, $text);
$text = self::addSitenameToPagetitle($text);
return $text;
}

Finally, this code worked :
$text = preg_replace_callback('/('.$replace2.')/',create_function('$replace','return #replace;'),$text);
Thank you all.

Related

php string replace not getting as desired

I have a string s follows:
«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«msup»«mi»x«/mi»«mn»2«/mn»«/msup»«/math»
I want to convert it to:
<math><msup><mi>x</mi><mn>2</mn></msup></math>
What I tried is as follows:
$text = str_replace("«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»","<math>", $text);
$text = str_replace("«/math»","</math>", $text);
$text = str_replace("»Â",">", $text);
$text = str_replace("«","<", $text);
echo $text;
But for my bad luck I am getting the output string as :
«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«msup»«mi»x«/mi»«mn»2«/mn»«/msup»«/math»
How can I make it?
There are just a couple str_replace's to do...
$text = "«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«msup»«mi»x«/mi»«mn»2«/mn»«/msup»«/math»";
$text = str_replace("«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»","<math>", $text);
$text = str_replace("«/math»","</math>", $text);
$text = str_replace("»Â",">", $text);
$text = str_replace("»",">", $text);
$text = str_replace("«","<", $text);
$text = str_replace("«","<", $text);
$text = str_replace("Â","", $text);
echo $text; // outputs <math><msup><mi>x</mi><mn>2</mn></msup></math>
You can use utf8_decode to remove the  symbol and replace all the unnecessary values using str_replace.
PHP Code
<?php
$text = utf8_decode("«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«msup»«mi»x«/mi»«mn»2«/mn»«/msup»«/math»");
$text = str_replace("«","<",$text);
$text = str_replace("»",">",$text);
$text = str_replace("xmlns=¨http://www.w3.org/1998/Math/MathML¨","",$text);
echo htmlspecialchars($text);
?>
Link::
Demo with source code in phpfiddle
Result::

php regex bbcode returns blank

I have this bbcode tag "remover" which should remove bbcode tags from my test text.
All i get is nothing. Just blank page where should be the text replaced with html tags.
Whats wrong with it. And maybe anyone have some better script to share.
$str = 'This [b]is just[/b] a [i]test[/i] text!';
function forum_text($str)
{
$str = htmlspecialchars($str);
$str = preg_replace( "#\[url\](?:http:\/\/)?(.+?)\[/url\]#is", "$1", $str );
$str = preg_replace( "#\[img\](?:http:\/\/)?(.+?)\[/img\]#is", "<img src=\"http://$1\" />", $str );
$str = preg_replace( "#\[b\](.+?)\[/b\]#is", "<strong>$1</strong>", $str );
$str = preg_replace( "#\[i\](.+?)\[/i\]#is", "<i>$1</i>", $str );
$str = preg_replace( "#\[u\](.+?)\[/u\]#is", "<u>$1</u>", $str );
return $str;
}
The following is your code, with some code in front of it (to make sure any errors are shown) and some code at the back (that actually calls your function).
If this doesn't work for you, your problem is not here, unless you don't have a working PCRE.
error_reporting(-1); ini_set('display_errors', 'On');
$str = 'This [b]is just[/b] a [i]test[/i] text!';
function forum_text($str)
{
$str = htmlspecialchars($str);
$str = preg_replace( "#\[url\](?:http:\/\/)?(.+?)\[/url\]#is", "$1", $str );
$str = preg_replace( "#\[img\](?:http:\/\/)?(.+?)\[/img\]#is", "<img src=\"http://$1\" />", $str );
$str = preg_replace( "#\[b\](.+?)\[/b\]#is", "<strong>$1</strong>", $str );
$str = preg_replace( "#\[i\](.+?)\[/i\]#is", "<i>$1</i>", $str );
$str = preg_replace( "#\[u\](.+?)\[/u\]#is", "<u>$1</u>", $str );
return $str;
}
echo forum_text($str);

replace everything except specific words in PHP

Is it possible to use php's preg_replace to remove anything in a string except specific words?
For example:
$text = 'Hello, this is a test string from php.';
I want to remove everything except "test" and "php" so it will be:
$text will be 'test php'
You could always use a callback. Under PHP 5.3:
$keep = array('test'=>1, 'php'=>1);
$text = trim(
preg_replace(
'/[^A-Za-z]+/', ' ',
preg_replace_callback(
'/[A-Za-z]+/',
function ($matched) use (&keep) {
if (isset($keep[$matched[0]])) {
return $matched[0];
}
return '';
}, $text
) ) );
Alternatively:
$text =
array_intersect(
preg_split('/[^A-Za-z]+/', $text),
array('test', 'php')
);
$text = 'Hello, this is a test string from php.';
$words = preg_split('~\W~', $text, -1, PREG_SPLIT_NO_EMPTY);
$allowed_words = array('test'=>1, 'php'=>1);
$output = array();
foreach($words as $word)
{
if(isset($allowed_words[$word]))
{
$output[] = $word;
}
}
print implode(' ', $output);

PHP Simple Wiki Parser requires NOWIKI Tag

I've got a simple function that converts plain text into a wiki markup. But I faced a problem where I'm unable to prevent certain strings from modification. So I need additional nowiki tag, within which no changes are made. I'm not very familiar with regex so help will be appreciated.
<?php
function simpleText($text){
$text = preg_replace('/======(.*?)======/', '<h5>$1</h5>', $text);
$text = preg_replace('/=====(.*?)=====/', '<h4>$1</h4>', $text);
$text = preg_replace('/===(.*?)===/', '<h3>$1</h3>', $text);
$text = preg_replace('/==(.*?)==/', '<h2>$1</h2>', $text);
$text = preg_replace('/==(.*?)==/', '<h1>$1</h1>', $text);
$text = preg_replace("/\*\*(.*?)\*\*/", '<b>$1</b>', $text);
$text = preg_replace("/__(.*?)__/", '<u>$1</u>', $text);
$text = preg_replace("/\/\/(.*?)\/\//", '<em>$1</em>', $text);
$text = preg_replace('/\-\-(.*?)\-\-/', '<strike>$1</strike>', $text);
$text = preg_replace('/\[\[Image:(.*?)\|(.*?)\]\]/', '<img src="$1" alt="$2" title="$2" />', $text);
$text = preg_replace('/\[(.*?) (.*?)\]/', '<a target="_blank" href="$1" title="$2">$2</a>', $text);
$text = preg_replace('/>(.*?)\n/', '<blockquote>$1</blockquote>', $text);
$text = preg_replace('/\* (.*?)\n/', '<ul><li>$1</li></ul>', $text);
$text = preg_replace('/<\/ul><ul>/', '', $text);
$text = preg_replace('/# (.*?)\n/', '<ol><li>$1</li></ol>', $text);
$text = preg_replace('/<\/ol><ol>/', '', $text);
$text = '<div class="wikiText">'.$text.'</div>';
return $text;
}
?>
You can use (?!ignore)
$text = preg_replace('(?!--)/======(.*?)======/(?!--)', '<h5>$1</h5>', $text);
Would exclude the header from being parsed if it was enclosed with '--' each end.

How to check if string contains any specified words then pass an error out of the function

I've been using the below code fine for a quick and efficient function of adding BB codes in my site.
function replace($text) {
//User Emotions
$text = str_replace(":)", "<img src=\"smiles/cool.gif\">", $text);
//User formatting
$text = str_replace("[center]", "<center>", $text);
$text = str_replace("[/center]", "</center>", $text);
$text = str_replace("[colour=red]", "<font color = red>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=blue]", "<font color = blue>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=green]", "<font color = green>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=orange]", "<font color = orange>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=white]", "<font color = white>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=black]", "<font color = black>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=code]", "<font color = code>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[b]", "<strong>", $text);
$text = str_replace("[/b]", "</strong>", $text);
$text = str_replace("[i]", "<i>", $text);
$text = str_replace("[/i]", "</i>", $text);
$text = str_replace("[u]", "<u>", $text);
$text = str_replace("[/u]", "</u>", $text);
$text = str_replace("[move]", "<marquee>", $text);
$text = str_replace("[/move]", "</marquee>", $text);
$text = str_replace("[img]", "<img border = \"0\" src = ", $text);
$text = str_replace("[/img]", ">", $text);
$text = str_replace("[code]", "<div id=code>", $text);
$text = str_replace("[/code]", "</div>", $text);
$text = str_replace(array("\r\n", "\n", "\r"), '<br />', $text);
//Racial Hatred Blocking
include("snippets/racial_violations.php");
return $text;
}
The question i wanted to ask is how would I go about checking if $text contained say:
"foo"
"yar"
"bar"
By passing my text var to the function (in a similar to the way i've done it above), but not replacing anything like str_replace does.
I want to then pass out an error from the function so i could use:
if($text_error == 1){ echo "text does not contain foo, yar or bar";}
else ( echo "text CONTAINS foo, yar or bar";}
$text_error would either be 0 or 1 and would be assigned this value if text contained one of the three specified words.
Hopefully I've explained this sufficiently!
You can perhaps edit the replace() function to add in the beginning a check for any such words.
function replace($text) {
$bannedWords = array('foo', 'yar', 'bar') ;
foreach ($bannedWords as $bannedWord) {
if (strpos($text, $bannedWord) !== false) {
return false ;
}
}
//..rest of str_replaces below here
return $text
}
In this case if one of the words is found the function will return the boolean false. You can check for that wherever you are calling replace(). If none are found then the text will be returned after replacing any BBcode, as before.
So,
$replacedText = replace($text) ;
if ($replacedText === false) {
//$text includes one of the bad words, act accordingly to inform the user
}
else {
//$text was ok, and has its bbcode replaced, use it accordingly
}
As for how to implement the above code more efficiently: str_replace also accepts arrays as its first two arguments:
$replace = array('[b]' => '<strong>', '[i]' => '<i>', ...);
$text = str_replace(array_keys($replace), array_values($replace), $text);
You can pass extra variable to function:
function replace($text, &$error) {
and set it later
$error = "text does not contain foo, yar or bar";

Categories