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
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;
?>

Link to comment
Share on other sites

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."

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.