How to do this PHP find-replace - php

I think I need to use the preg_replace function but not sure exactly how to type in the patterns I want to find and replace. Basically, I want to replace this:
: u"x"x",
with this:
: u"x'x",
x means that any characters can go there. But I don't know how to write the x in PHP.
Thank you!
Edit: basically, I want to replace that middle double-quote with a single-quote. And I'll be searching through a big JSON file to do it. Probably should have said this at the start.

You could use this regular expression:
$result = preg_replace('#(: u".*?)"(.*?")#', "$1'$2", $string);

Related

preg_replace a string that contains vertical line/pipe character

I have strings like this:
$str1="YESYES|c|no|c|";
$str2="YESYES|c|not this|c|YES|c|or this|c|";
$str3="YES";
I want strings like this:
$str1="YESYES";
$str2="YESYESYES";
$str3="YES";
I thought I could use preg_replace but the fact that I'm looking at a pipe seems to cause trouble, if I go like this:
$i=preg_replace("//|c\|[\s\S]+?/|c\|/",'',$i);
I get an 'unknown modifier "|"' error. I know this must have been asked before but it's very hard to search for. What is the proper regex to use?
You have to use the next line:
$i=preg_replace("/\|c\|[\s\S]+?\|c\|/",'',$i);
which is almost identical to what you have, but using \ instead of / to scape the pipes

Using preg_match_all to filter out strings containing this but not this

im having an issue with preg_match_all. I have this string:
$product_req = "ACTIVE-6,CATEGORY-ACTIVE-8,CATEGORY-ACTIVE-4,ACTIVE-9";
I need to get the numbers preceded by "ACTIVE-" but not by "CATEGORY-ACTIVE-", so in this case the result should be 6,9. I used the statement below:
preg_match_all("/ACTIVE-(\d+)/", $product_req, $this_act);
However this will return all the numbers because all of them are in fact preceded by "ACTIVE-" but thats not what i meant because i need to leave out those preceded by "CATEGORY-ACTIVE-". How can i configure preg_match_all to do it? Or maybe there is some other function that can do the job?
EDIT:
I tried this:
preg_match_all("/CATEGORY-ACTIVE-(\d+)/", $product_req, $this_cat_act);
preg_match_all("/ACTIVE-(\d+)/", $product_req, $this_act);
$act_cat = str_replace($this_cat_act[1],"",$this_act[1]);
it kinda works, but i guess there is a better and cleaner way to do it. Besides the output is kinda weird too.
Thank you.

preg_match and remove multiple characters in string?

Hi I'm using php to program my site and I've been reading loads about preg_match and trying lots of examples. What I'm trying to do is something like below...
if(preg_match('These characters'), $myVariable, matches)){
Find and remove found characters from $myVariable;
}
I'm pretty sure this is obvious to php experts but it's had me stuck after hours of trying and reading.
Thanks in advance
You don't need to check for a match before doing a replace. It's like if you were to do:
str_replace("A","B","ZZZZZZZ");
It just won't replace anything. Same goes for preg_replace: If there is no match, it just does nothing.
It sounds like you should be using preg_replace. If you wanted to remove all y's and o's for example you would do this:
$string = 'hey you guys!';
$ans = preg_replace('/[yo]/','',$string);
print_r($ans); //outputs 'he u gus!'
Whatever characters you want to remove, just put them between the brackets [...]

Regex to change spaces in images into entities

I'm having a lot of difficulty matching an image url with spaces.
I need to make this
http://site.com/site.com/files/images/img 2 (5).jpg
into a div like this:
.replace(/(http:\/\/([^\s]+\.(jpg|png|gif)))/ig, "<div style=\"background: url($1)\"></div>")
Here's the thread about that:
regex matching image url with spaces
Now I've decided to first make the spaces into entities so that the above regex will work.
But I'm really having a lot of difficulty doing so.
Something like this:
.replace(/http:\/\/(.*)\/([^\<\>?:;]*?) ([^\<\>?:;]*)(\.(jpe?g|png|gif))/ig, "http://$1/$2%20$3$4")
Replaces one space, but all the rest are still spaces.
I need to write a regex that says, make all spaces between http:// and an image extension (png|jpg|gif) into %20.
At this point, frankly not sure if it's even possible. Any help is appreciated, thanks.
Trying Paolo's escape:
.escape(/http:\/\/(.*)\/([^\<\>?:;]*?) ([^\<\>?:;]*)(\.(jpe?g|png|gif))/)
Another way I can do this is to escape serverside in PHP, and in PHP I can directly mess with the file name without having to match it in regex.
But as far as I know something like htmlentities do not apply to spaces. Any hints in this direction would be great as well.
Try the escape function:
>>> escape("test you");
test%20you
If you want to control the replacement character but don't want to use a regular expression, a simple...
$destName = str_replace(' ', '-', $sourceName);
..would probably be the more efficient solution.
Lets say you have the string variable urlWithSpaces which is set to a URL which contains spaces.
Simply go:
urlWithoutSpaces = escape(urlWithSpaces);
What about urlencode() - that may do what you want.
On the JS side you should be using encodeURI(), and escape() only as a fallback. The reason to use encodeURI() is that it uses UTF-8 for encoding, while escape() uses ISO Latin. Same problems applies for decoding.
encodeURI = encodeURI || escape;
alert(encodeURI('image name.png'));

php preg_match two examples

I need to preg_match for
src="http:// "
where the blank space following // is the rest of the url ending with the ". My adapted doesn't seem to work:
preg_match('#src="(http://[^"]+)#', $data, $match);
And I am also struggling to get text that starts with > and ends with EITHER a full stop . or an exclamation mark ! or a question mark ? I have no idea how to do this one. An example of the text I want to preg_match for is:
blahblahblah>Hello world this is what I want.
I'm hoping a kind preg_match guru can tell me the answer and save me hours of headscratching.
Thanks for reading.
As for the URL:
preg_match('#src="(.*?)"#', $data, $match);
and for the second case, use />(.*?)(\.|!|\?)/
(.*?)" will match any character greedily up until the time it sees the end double quote
It seems that you want to parse a document or string which follows a HTML, DOM, XML or something similiar structure.
Use XPath, and parse to the Tag and let it return the src Attribute, this will save much trouble and you can forget about regular expressions.
Example: CLICK ME

Categories