Replace a single quote ( ' ) with another quote (’) str_replace - php

I am trying to replace a single quote (') with another quote (’), but I can't seem to get anything to work.
Also, how can I make this work on multiple strings ($text, $title, $notice)?
input: don't
output: don’t
I am trying this:
$text = str_replace(array("'",'"'), array('’'), $text);
$text = htmlentities(str_replace(array('"', "'"), '’', $text);
$text = htmlentities(str_replace(array('"', "'"), '’', $_POST['text']));
$text = str_replace("'" ,"’",$text);
$text = str_replace("'" ,"’",$text);
$text = str_replace(array("'"), "’", $text);
$text = str_replace(array("\'", "'", """), "’", htmlspecialchars($text));
$text = str_replace(array('\'', '"'), '’', $text);
$text = str_replace(chr(39), chr(146), $text);
$text = str_replace("'", "&ampquot;", $text);
None of this works.

When you use an array as replacements, give as many replacements as you give needles. Else use a simple string.
<?php
$text = 'Peter\'s cat\'s name is "Tom".';
$text = str_replace(array("'",'"'), '’', $text);
echo $text;
$text = 'Peter\'s cat\'s name is "Tom".';
$text = str_replace(array("'",'"'), array('’', '`'), $text);
echo $text;
?>
To perform that task on multiple variables you could do
<?php
$text = 'Peter\'s cat\'s name is "Tom".';
$title = 'Peter\'s cat\'s name is "Tom".';
$notice = 'Peter\'s cat\'s name is "Tom".';
foreach([&$text, &$title, &$notice] as &$item)
$item = str_replace(array("'",'"'), '’', $item);
echo "text: $text<br>title: $title<br>notice: $notice";
?>

I tried using preg_replace() and it worked perfectly first time:
$text = "Someone told me about a 'bacon and cheese sandwich'";
$text = preg_replace("#\'#", '’', $text);
echo $text;
Output
Someone told me about a ’bacon and cheese sandwich’
Give that a go.

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::

How can I replace ":" with "/" in slugify function?

I have a function which slugifies the text, it works well except that I need to replace ":" with "/". Currently it replaces all non-letter or digits with "-". Here it is :
function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
if (function_exists('iconv'))
{
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
}
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
if (empty($text))
{
return 'n-a';
}
return $text;
}
I made just a couple modifications. I provided a search/replace set of arrays to let us replace most everything with -, but replace : with /:
$search = array( '~[^\\pL\d:]+~u', '~:~' );
$replace = array( '-', '/' );
$text = preg_replace( $search, $replace, $text);
And later on, this last preg_replace was replacing our / with an empty string. So I permited foward slashes in the character class.
$text = preg_replace('~[^-\w\/]+~', '', $text);
Which outputs the following:
// antiques/antiquities
echo slugify( "Antiques:Antiquities" );

Remove non-ascii characters from string

I'm getting strange characters when pulling data from a website:
Â
How can I remove anything that isn't a non-extended ASCII character?
A more appropriate question can be found here:
PHP - replace all non-alphanumeric chars for all languages supported
A regex replace would be the best option. Using $str as an example string and matching it using :print:, which is a POSIX Character Class:
$str = 'aAÂ';
$str = preg_replace('/[[:^print:]]/', '', $str); // should be aA
What :print: does is look for all printable characters. The reverse, :^print:, looks for all non-printable characters. Any characters that are not part of the current character set will be removed.
Note: Before using this method, you must ensure that your current character set is ASCII. POSIX Character Classes support both ASCII and Unicode and will match only according to the current character set. As of PHP 5.6, the default charset is UTF-8.
You want only ASCII printable characters?
use this:
<?php
header('Content-Type: text/html; charset=UTF-8');
$str = "abqwrešđčžsff";
$res = preg_replace('/[^\x20-\x7E]/','', $str);
echo "($str)($res)";
Or even better, convert your input to utf8 and use phputf8 lib to translate 'not normal' characters into their ascii representation:
require_once('libs/utf8/utf8.php');
require_once('libs/utf8/utils/bad.php');
require_once('libs/utf8/utils/validation.php');
require_once('libs/utf8_to_ascii/utf8_to_ascii.php');
if(!utf8_is_valid($str))
{
$str=utf8_bad_strip($str);
}
$str = utf8_to_ascii($str, '' );
$clearstring=filter_var($rawstring, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
UPDATE:
FILTER_SANITIZE_STRING is deprecated since PHP 8.1
https://www.php.net/manual/en/migration81.deprecated.php#migration81.deprecated.filter
Kind of related, we had a web application that had to send data to a legacy system that could only deal with the first 128 characters of the ASCII character set.
Solution we had to use was something that would "translate" as many characters as possible into close-matching ASCII equivalents, but leave anything that could not be translated alone.
Normally I would do something like this:
<?php
// transliterate
if (function_exists('iconv')) {
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
}
?>
... but that replaces everything that can't be translated into a question mark (?).
So we ended up doing the following. Check at the end of this function for (commented out) php regex that just strips out non-ASCII characters.
<?php
public function cleanNonAsciiCharactersInString($orig_text) {
$text = $orig_text;
// Single letters
$text = preg_replace("/[∂άαáàâãªä]/u", "a", $text);
$text = preg_replace("/[∆лДΛдАÁÀÂÃÄ]/u", "A", $text);
$text = preg_replace("/[ЂЪЬБъь]/u", "b", $text);
$text = preg_replace("/[βвВ]/u", "B", $text);
$text = preg_replace("/[çς©с]/u", "c", $text);
$text = preg_replace("/[ÇС]/u", "C", $text);
$text = preg_replace("/[δ]/u", "d", $text);
$text = preg_replace("/[éèêëέëèεе℮ёєэЭ]/u", "e", $text);
$text = preg_replace("/[ÉÈÊË€ξЄ€Е∑]/u", "E", $text);
$text = preg_replace("/[₣]/u", "F", $text);
$text = preg_replace("/[НнЊњ]/u", "H", $text);
$text = preg_replace("/[ђћЋ]/u", "h", $text);
$text = preg_replace("/[ÍÌÎÏ]/u", "I", $text);
$text = preg_replace("/[íìîïιίϊі]/u", "i", $text);
$text = preg_replace("/[Јј]/u", "j", $text);
$text = preg_replace("/[ΚЌК]/u", 'K', $text);
$text = preg_replace("/[ќк]/u", 'k', $text);
$text = preg_replace("/[ℓ∟]/u", 'l', $text);
$text = preg_replace("/[Мм]/u", "M", $text);
$text = preg_replace("/[ñηήηπⁿ]/u", "n", $text);
$text = preg_replace("/[Ñ∏пПИЙийΝЛ]/u", "N", $text);
$text = preg_replace("/[óòôõºöοФσόо]/u", "o", $text);
$text = preg_replace("/[ÓÒÔÕÖθΩθОΩ]/u", "O", $text);
$text = preg_replace("/[ρφрРф]/u", "p", $text);
$text = preg_replace("/[®яЯ]/u", "R", $text);
$text = preg_replace("/[ГЃгѓ]/u", "r", $text);
$text = preg_replace("/[Ѕ]/u", "S", $text);
$text = preg_replace("/[ѕ]/u", "s", $text);
$text = preg_replace("/[Тт]/u", "T", $text);
$text = preg_replace("/[τ†‡]/u", "t", $text);
$text = preg_replace("/[úùûüџμΰµυϋύ]/u", "u", $text);
$text = preg_replace("/[√]/u", "v", $text);
$text = preg_replace("/[ÚÙÛÜЏЦц]/u", "U", $text);
$text = preg_replace("/[Ψψωώẅẃẁщш]/u", "w", $text);
$text = preg_replace("/[ẀẄẂШЩ]/u", "W", $text);
$text = preg_replace("/[ΧχЖХж]/u", "x", $text);
$text = preg_replace("/[ỲΫ¥]/u", "Y", $text);
$text = preg_replace("/[ỳγўЎУуч]/u", "y", $text);
$text = preg_replace("/[ζ]/u", "Z", $text);
// Punctuation
$text = preg_replace("/[‚‚]/u", ",", $text);
$text = preg_replace("/[`‛′’‘]/u", "'", $text);
$text = preg_replace("/[″“”«»„]/u", '"', $text);
$text = preg_replace("/[—–―−–‾⌐─↔→←]/u", '-', $text);
$text = preg_replace("/[ ]/u", ' ', $text);
$text = str_replace("…", "...", $text);
$text = str_replace("≠", "!=", $text);
$text = str_replace("≤", "<=", $text);
$text = str_replace("≥", ">=", $text);
$text = preg_replace("/[‗≈≡]/u", "=", $text);
// Exciting combinations
$text = str_replace("ыЫ", "bl", $text);
$text = str_replace("℅", "c/o", $text);
$text = str_replace("₧", "Pts", $text);
$text = str_replace("™", "tm", $text);
$text = str_replace("№", "No", $text);
$text = str_replace("Ч", "4", $text);
$text = str_replace("‰", "%", $text);
$text = preg_replace("/[∙•]/u", "*", $text);
$text = str_replace("‹", "<", $text);
$text = str_replace("›", ">", $text);
$text = str_replace("‼", "!!", $text);
$text = str_replace("⁄", "/", $text);
$text = str_replace("∕", "/", $text);
$text = str_replace("⅞", "7/8", $text);
$text = str_replace("⅝", "5/8", $text);
$text = str_replace("⅜", "3/8", $text);
$text = str_replace("⅛", "1/8", $text);
$text = preg_replace("/[‰]/u", "%", $text);
$text = preg_replace("/[Љљ]/u", "Ab", $text);
$text = preg_replace("/[Юю]/u", "IO", $text);
$text = preg_replace("/[fifl]/u", "fi", $text);
$text = preg_replace("/[зЗ]/u", "3", $text);
$text = str_replace("£", "(pounds)", $text);
$text = str_replace("₤", "(lira)", $text);
$text = preg_replace("/[‰]/u", "%", $text);
$text = preg_replace("/[↨↕↓↑│]/u", "|", $text);
$text = preg_replace("/[∞∩∫⌂⌠⌡]/u", "", $text);
//2) Translation CP1252.
$trans = get_html_translation_table(HTML_ENTITIES);
$trans['f'] = 'ƒ'; // Latin Small Letter F With Hook
$trans['-'] = array(
'…', // Horizontal Ellipsis
'˜', // Small Tilde
'–' // Dash
);
$trans["+"] = '†'; // Dagger
$trans['#'] = '‡'; // Double Dagger
$trans['M'] = '‰'; // Per Mille Sign
$trans['S'] = 'Š'; // Latin Capital Letter S With Caron
$trans['OE'] = 'Œ'; // Latin Capital Ligature OE
$trans["'"] = array(
'‘', // Left Single Quotation Mark
'’', // Right Single Quotation Mark
'›', // Single Right-Pointing Angle Quotation Mark
'‚', // Single Low-9 Quotation Mark
'ˆ', // Modifier Letter Circumflex Accent
'‹' // Single Left-Pointing Angle Quotation Mark
);
$trans['"'] = array(
'“', // Left Double Quotation Mark
'”', // Right Double Quotation Mark
'„', // Double Low-9 Quotation Mark
);
$trans['*'] = '•'; // Bullet
$trans['n'] = '–'; // En Dash
$trans['m'] = '—'; // Em Dash
$trans['tm'] = '™'; // Trade Mark Sign
$trans['s'] = 'š'; // Latin Small Letter S With Caron
$trans['oe'] = 'œ'; // Latin Small Ligature OE
$trans['Y'] = 'Ÿ'; // Latin Capital Letter Y With Diaeresis
$trans['euro'] = '€'; // euro currency symbol
ksort($trans);
foreach ($trans as $k => $v) {
$text = str_replace($v, $k, $text);
}
// 3) remove <p>, <br/> ...
$text = strip_tags($text);
// 4) & => & " => '
$text = html_entity_decode($text);
// transliterate
// if (function_exists('iconv')) {
// $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// }
// remove non ascii characters
// $text = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $text);
return $text;
}
?>
I also think that the best solution might be to use a regular expression.
Here's my suggestion:
function convert_to_normal_text($text) {
$normal_characters = "a-zA-Z0-9\s`~!##$%^&*()_+-={}|:;<>?,.\/\"\'\\\[\]";
$normal_text = preg_replace("/[^$normal_characters]/", '', $text);
return $normal_text;
}
Then you can use it like this:
$before = 'Some "normal characters": Abc123!+, some ASCII characters: ABC+ŤĎ and some non-ASCII characters: Ąąśćł.';
$after = convert_to_normal_text($before);
echo $after;
Displays:
Some "normal characters": Abc123!+, some ASCII characters: ABC+ and some non-ASCII characters: .
I just had to add the header
header('Content-Type: text/html; charset=UTF-8');
This should be pretty straight forwards and no need for iconv function:
// Remove all characters that are not the separator, a-z, 0-9, or whitespace
$string = preg_replace('![^'.preg_quote('-').'a-z0-_9\s]+!', '', strtolower($string));
// Replace all separator characters and whitespace by a single separator
$string = preg_replace('!['.preg_quote('-').'\s]+!u', '-', $string);
My problem is solved
$text = 'Châu Thái Nhân 12/09/2022';
echo preg_replace('/[\x00-\x1F\x7F]/', '', $text);
//Châu Thái Nhân 12/09/2022
I think the best way to do something like this is by using ord() command. This way you will be able to keep characters written in any language. Just remember to first test your text's ord results. This will not work on unicode.
$name="βγδεζηΘKgfgebhjrf!##$%^&";
//this function will clear all non greek and english characters on greek-iso charset
function replace_characters($string)
{
$str_length=strlen($string);
for ($x=0;$x<$str_length;$x++)
{
$character=$string[$x];
if ((ord($character)>64 && ord($character)<91) || (ord($character)>96 && ord($character)<123) || (ord($character)>192 && ord($character)<210) || (ord($character)>210 && ord($character)<218) || (ord($character)>219 && ord($character)<250) || ord($character)==252 || ord($character)==254)
{
$new_string=$new_string.$character;
}
}
return $new_string;
}
//end function
$name=replace_characters($name);
echo $name;

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