Jump to content

replace all single quote string


gacon13

Recommended Posts

I have many string which has single quote string. e.g:

abc 'deg' tyui 'tr23' fgh dfgd f sdfg df gdf dfg dfg  ...

 

What I want to do is a function that search through the text then find everywhere that has single quote string and replace with a pre-define string, in above e.g i want to replace 'deg' with 'MY_PRE_DEFINE' and 'tr23' with 'MY_PRE_DEFINE' but I have not found the solution yet.

 

Please help.

 

Thanks in advanced.

Link to comment
https://forums.phpfreaks.com/topic/118056-replace-all-single-quote-string/
Share on other sites

Here try this out:

<?php
$str = "abc 'deg' tyui 'tr23' fgh dfgd f sdfg df gdf dfg dfg  ...";

$search = array(
							"#\'deg\'#",
							"#\'tr23\'#",
							);
$replace = array(
							"DEG_REPLACER_HERE",
							"TR23_REPLACER_HERE",
								);

$str = preg_replace($search,$replace,$str);
echo $str;
?>

Thank you for your reply.

 

But the string in the quote is unknown. I mean the input text is changed and i will not know what is in the quote.

 

For example:

text1 = "But the 'string in' the quote is unknown. 'I mean' the input text is changed and i will not know what is in the quote."

text2 = "But the string in the quote 'is unknown'. I mean the input text is changed and i will not know 'what is in the' quote."

 

Then whatever the input is, I want to find all things that is quoted by single quote and replace with 'MY_PRE_DEFINE'

The output will be:

text1 = "But the 'MY_PRE_DEFINE' the quote is unknown. 'MY_PRE_DEFINE' the input text is changed and i will not know what is in the quote."

text2 = "But the string in the quote 'MY_PRE_DEFINE'. I mean the input text is changed and i will not know 'MY_PRE_DEFINE' quote."

Like this?

<?php
$text1 = "But the 'string in' the quote is unknown. 'I mean' the input text is changed and i will not know what is in the quote.";
$text2 = "But the string in the quote 'is unknown'. I mean the input text is changed and i will not know 'what is in the' quote.";

$text1 = preg_replace("#\'(.*?)\'#","'MY_PRE_DEFINE'",$text1);
echo $text1;
?>

Thank you a lot.

 

I still do not understand preg_replace pattern, I use

$pattern = "#'.*?'#s";

the result is the same.

 

Now I'm trying to find what has been replaced, e.g: MY_PRE_DEFINE = tr23

I saved the input text into 2 files then do a search to find what could help to find the different and found that php diff can do. But I just want to return the array of the input and output string only, not the whole content. How could I do that?

<pre>
<?php
$data = <<<DATA
	But the 'string in' the quote is unknown. 'I mean' the input text is changed and i will not know what is in the quote.
	But the string in the quote 'is unknown'. I mean the input text is changed and i will not know 'what is in the' quote.
DATA;

$finds = array();
$data = preg_replace("/'([^']*)'/e", "
	### The ternary is only used in order to pull off
	### the arraying and the replacing. I couldn't get
	### it to work any other way.
	array_push(\$finds, '$1') ? 'MY_PRE_DEFINE' : '$1' ;
", $data);

echo $data, '<hr>';
print_r($finds);
?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.