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.
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.
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."
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:
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?
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
#!/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);
}
?>
. 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
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?
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>
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>