Page 1 of 1

replace all single quote string

Posted: Mon Aug 04, 2008 4:07 am
by gacon13
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.

Re: replace all single quote string

Posted: Mon Aug 04, 2008 4:11 am
by jaoudestudios
Can you give an example of before and after?

You can easily encode all single quotes if it is breaking your php and mysql.

Re: replace all single quote string

Posted: Mon Aug 04, 2008 4:38 am
by prometheuzz
gacon13 wrote: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.

Code: Select all

echo preg_replace("/'(?:deg|tr23)'/", "'MY_PRE_DEFINE'", "abc 'deg' tyui 'tr23' fgh dfgd f sdfg df gdf dfg dfg ...");

Re: replace all single quote string

Posted: Mon Aug 04, 2008 9:37 pm
by gacon13
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."

Re: replace all single quote string

Posted: Tue Aug 05, 2008 2:12 am
by prometheuzz
Ah, I see what you mean. This will do just that:

Code: Select all

echo preg_replace("/'[^']++'/", "'MY_PRE_DEFINE'", 
    "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.");
echo preg_replace("/'[^']++'/", "'MY_PRE_DEFINE'", 
    "\nBut the string in the quote 'is unknown'. I mean the input text is changed and i will not know 'what is in the' quote.");
Note that the code above will not replace two successive single quotes; there must be one or more characters between them. If you want to replace double single quotes as well, use this regex instead:

Code: Select all

"/'[^']*+'/"
Good luck.

Re: replace all single quote string

Posted: Tue Aug 05, 2008 3:10 am
by gacon13
Thanks a lot.

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?

Re: replace all single quote string

Posted: Tue Aug 05, 2008 3:16 am
by prometheuzz
gacon13 wrote:Thanks a lot.

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?
Huh? I don't really know what it is you're trying to do now.
If you still have a question about regex, feel free to post it here. But, if you have a question about some other PHP function, I suggest you post it in the general PHP section:
viewforum.php?f=1

Good luck!

Re: replace all single quote string

Posted: Tue Aug 05, 2008 3:42 am
by prometheuzz
Perhaps you meant something like this?

Code: Select all

#!/usr/bin/php
<?php
if(preg_match_all("/'[^']++'/", 
        "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.", 
        $matches)) {
    print_r($matches);
}
if(preg_match_all("/'[^']++'/", 
        "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.", 
        $matches)) {
    print_r($matches);
}
?>

Re: replace all single quote string

Posted: Tue Aug 05, 2008 4:01 am
by gacon13
Greate thanks. It is what i'm looking for.

Could you tell me what is the different between your pattern and this pattern:

Code: Select all

"#'.*?'#s"
Where can I learn the format of preg_.. pattern?

Thank you again.

Re: replace all single quote string

Posted: Tue Aug 05, 2008 4:22 am
by prometheuzz
gacon13 wrote:Greate thanks. It is what i'm looking for.
Good to hear that.
gacon13 wrote:Could you tell me what is the different between your pattern and this pattern:

Code: Select all

"#'.*?'#s"
Note that

Code: Select all

'/pattern/s'
is the same as

Code: Select all

'#pattern#s'
. The s-flag at the end tells the regex engine to let the . (dot) character match all kind of characters, including new-line characters. Without that flag, the . (dot) would not match a new line character (among others).
Now, the difference between

Code: Select all

'.*?'
and

Code: Select all

'[^']++'
 
is subtle and cannot easily be explained to someone with little knowledge in regex. What I can tell you is that the latter one is faster (especially on larger strings) and should be preferred. To learn the details about it, see:
http://www.regular-expressions.info/repeat.html
Especially the paragraphs "Watch Out for The Greediness!", "Laziness Instead of Greediness" and "An Alternative to Laziness" explain the real difference between the two.
gacon13 wrote:Where can I learn the format of preg_.. pattern?
A good web resource is:
http://www.regular-expressions.info (general regex info)
http://www.regular-expressions.info/php.html (php's regex functions)

And if you're ever going to buy a book about regex, this is the one:
Mastering Regular Expressions by J. Friedl, http://regex.info/
gacon13 wrote:Thank you again.
You're most welcome.

Re: replace all single quote string

Posted: Sun Aug 10, 2008 7:02 am
by gacon13
Hi again,

After a few days working on the input text. I found some case cause the result different from what i want, for example: she's... created unwanted single quote. So I decide to change single quote into a tag, e.g: 'need to be replace' will become <mytag>need to be replaced</mytag>

Could you help me to change to pattern?

Thank you.

Re: replace all single quote string

Posted: Sun Aug 10, 2008 7:18 am
by prometheuzz
gacon13 wrote:Hi again,

After a few days working on the input text. I found some case cause the result different from what i want, for example: she's... created unwanted single quote. So I decide to change single quote into a tag, e.g: 'need to be replace' will become <mytag>need to be replaced</mytag>

Could you help me to change to pattern?

Thank you.
Try this pattern:

Code: Select all

'#<mytag>.*?</mytag>#is'

Re: replace all single quote string

Posted: Sun Aug 10, 2008 8:32 am
by gacon13
Thanks a lot :D