Usin Preg_match_all with "\" character - php

I am trying to get a expression from a string using preg_match_all
but it doesn´t work correctly I try this:
$string='this is a test hello \1234';
preg_match_all('[(\\)(0-9)], $string, $output);
It only shows "hello" if I quit the "\" of the $string I can see "hello 1234" but with "\" It doesn´t works.

i think i might have a solution for you
$string='this is a test hello \1234';
preg_match("/hello \\\\[0-9]*/", $string, $output);
var_dump($output);
the thing here are the 4 backslashes. I'm not 100% sure about this but i think it is because in php you need to escape backslash so \\ produces \
and in the regular expression you need two backslashes as well (see praveen kumars answer for a good regexp explanation). So when you are using 4 backslashes, php changes that to 2 and that is what you need for this regexp
EDIT:
turns out there was some truth in my thoughts. check out this article for more explanation

If you use something like this:
/^(hello \\[0-9]*)$/g
This will give you the whole one. See the ( and [ variations.
Explanation

Related

preg_match regex, find text within link

Im looking to 'preg_match' a link that looks like /dp/B0039SD7S6/blah-blah my current expression looks like...
$var = preg_match('/dp\/(.?*)\//', $output);
This doesn't output '039SD7S6'. Im assuming because the backslashes interfere with the delimiter. Help would be appreciated, Thanks.
There are 3 problems with your regex:
yes, the slash, you have to escape it or change delimiters;
ungreedy option: ? must be after .*
preg_match syntax: result must be in parameters, not in returning value.
Change in this way:
preg_match('/dp\/(.*?)\//', $output, $var);
Or - as I prefer - in this way:
preg_match('{dp/(.*?)/}', $output, $var);
See more about preg_match()
See more about Regular expression syntax

Need to match multiple backslahes in a PHP regex

I'm trying to match 4 backslahes using preg_match.
preg_match('/\\\\\\\\/',$subject) works fine, but preg_match('/\\{4}/',$subject) doesn't.
Perhaps I'm using {} incorrectly. Could anyone advise?
Ok I got it: Two backslashes mean you want one backslash in your string: So for the regex it looks like this: /\{4}/ Which means you want to escape the {
What you need here is:
preg_match('/\\\\{4}/', $subject);
That looks for the regex like this: /\\{4}/ and works properly.
Regex is the wrong tool when you're looking for a literal string.
strpos($subject, str_repeat("\\",4)) !== false
Use this:
preg_match('/(?:\\\\){2}/',$subject, $m);
It matches 4 backslashes.

preg_match dose not work correctly in php

all,
I am using preg_match to filter some data, and it is strange that, it dose not work correctly. I am new to regex, and I used a php live regex website to check my regex, which works correctly. So I have no idea what is wrong here.
I would like to have preg_match to find something like "a\_b" in the $string:
$string="aaa\_bbb:ccc"
if(preg_match("/[a-zA-Z]\\_[a-zA-Z]/", $string)){
$snew = str_replace('\_', "_", $string);
}
But it is strange that even I have a $string like in this example above, the result of preg_match is 0. But when I change it to
preg_match("/\\_[a-zA-Z]/", $string)
It works fine and return 1. But of course that is not what I want. Any idea?
Thanks very much~
You don't really need the preg_match at all, from what I can see.
However the problem you're having with it is to do with escaping.
You have this: "/[a-zA-Z]\\_[a-zA-Z]/"
You've correctly identified that the backslash needs to be escaped, however, you've missed a subtle issue:
Regular expressions in PHP are strings. This means that you need to escape it as a string as well as a regular expression. In effect, this means that to correctly escape a backslash so it is matched as an actual backslash character in your pattern, you actually need to have four backslashes.
"/[a-zA-Z]\\\\_[a-zA-Z]/"
It's not pretty, but that's how it is.
Hope that helps.
use:
if(preg_match("/[a-zA-Z]\\\\_[a-zA-Z]/", $string))
instead
You don't need the preg_match altogether, instead just do a replace using this regex:
/([a-zA-Z])\\\\_([a-zA-Z])/
and then replace with $1_$2, like this:
$result = preg_replace("/([a-zA-Z])\\\\_([a-zA-Z])/", "$1_$2$, $string);

Exact string replacement in php

I'm looking for a way to replace a string in php that exactly matches with the subject.
For example I got a file named 'hello-world.txt' having three lines:
'http://www.example.com/'
'http://www.example.com/category/'
'http://www.example.com/tag/name/'
and I need to replace the 'http://www.example.com/' with 'http://www.example2.com'
$string=file_get_contents('hello-world.txt');
$string=str_replace('http://www.example.com/','http://www.example2.com',$string);
echo $string;
I will be getting a result similar to this:
'http://www.example2.com/'
'http://www.example2.com/category/'
'http://www.example2.com/tag/name/'
But What I actually need is something like this:
'http://www.example2.com/'
'http://www.example.com/category/'
'http://www.example.com/tag/name/'
Please Help!!!!
You can use preg_replace with the m modifier as:
$string=preg_replace('~^http://www\.example\.com/$~m','http://www.example2.com',$string);
Code In Action
First check if the current line is what you're looking for. If not, just spit it back out.
Either $string=str_replace("'http://www.example.com/'", "'http://www.example2.com'", $string); since in your example you have single quotes around each line or use preg_replace like this:
$string=preg_replace('/^http:\/\/www\.example\.com\/$/', 'http://www.example2.com/', $string);
... if those single quotes aren't supposed to be there. The $ at the end of the regex means the end of the line and the ^ means the beginning of the line. Periods and / need to be escaped hence the \. and \/
I haven't tested this code. Here is a link to preg_replace() http://php.net/manual/en/function.preg-replace.php

PHP: Preg replace string with nothing

I'm new to preg_replace() and I've been trying to get this to work, I couldn't so StackOverflow is my last chance.
I have a string with a few of these:
('pm_IDHERE', 'NameHere');">
I want it to be replaced with nothing, so it would require 2 wildcards for NameHere and pm_IDHERE.
But I've tried it and failed myself, so could someone give me the right code please, and thanks :)
Update:
You are almost there, you just have to make the replacement an empty string and escape the parenthesis properly, otherwise they will be treated as capture group (which you don't need btw):
$str = preg_replace("#\('pm_.+?', '.*?'\);#si", "", $str);
You probably also don't need the modifiers s and i but that is up to you.
Old answer:
Probably str_replace() is sufficient:
$str = "Some string that contains pm_IDHERE and NameHere";
$str = str_replace(array('pm_IDHERE', 'NameHere'), '', $str);
If this is not what you mean and pm_IDHERE is actually something like pm_1564 then yes, you probably need regular expressions for that. But if NameHere has no actual pattern or structure, you cannot replace it with regular expression.
And you definitely have to explain better what kind of string you have and what kind of string you have want to replace.

Categories