Hello i have this code but there is a problem with preg_match on passing $pattern variable.
$pattern = htmlspecialchars($_POST['pregmatch']);
$pattern = "'".$pattern."'";
preg_match($pattern, $content, $m);
if i give this /<span class=\"product_title\">(.*)<\/span>/
in the $_POST['pregmatch'] pregmatch returns NULL.
If i change the code to this
//$pattern = htmlspecialchars($_POST['pregmatch']);
//$pattern = "'".$pattern."'";
preg_match('/<span class=\"product_title\">(.*)<\/span>/', $content, $m);
works like a charm.
So whats the deal here? Slashes creates this problem?
Thanks in advance.
Both of your additional lines appear to be causing a problem... if you were to do:
preg_match($_POST['pregmatch'], $content, $m);
You would probably get the desired result.
htmlspecialchars converts the angle brackets < and > from your input into the HTML escapes < and >. Thus your pattern becomes:
/<span class=\"product_title">(.*)<\/span>/
Which won't match the $content if that's still raw HTML.
Also adding ' around the pattern will screw it up:
$pattern = "'".$pattern."'";
You then had two delimiters.
'/<span class="product_title">(.*)<\/span>/'
Best idea is to use var_dump when unsure about variable state. And enable error_reporting whenever something doesn't work.
Related
Sorry for my english.
$string = "product#[:id]#[:str]";
$regex = preg_replace("/\[:(\w+)\]/", "(?<$1>.+?)", $string);
What I get is product#(?.+?)#(?.+?) instead of product#(?<id>.+?)#(?<str>.+?)
I want to do that because I need preg_match() to create key names in $matches:
preg_match('/^'. $regex .'$/', str_replace("/", "#", self::$path), $matches)
The point is, it works without '<' and '>'. But I need them for next code.
So what should I do about these '<', '>' chars? I need them but don't know what to do. I also tried to escape them: \< (But no results).
Please help me if you have any ideas.
Edit:
Seems like what I did was ok.
Now there's new problem:
$string = "product#[i:id]#[*:str]";
$regex = preg_replace("/\[i:(\w+)\]/", "(?<$1>[0-9]+)", $regex);
$regex = preg_replace("/\[\*:(\w+)\]/", "(?<$1>.+?)", $regex);
No I tried something else and this doesn't work. Why? :(
If the result of your preg_replace is to be rendered in a browser,
and just this rendered result should contain < and > chars,
then you should generate them as < and >.
So maybe your replacement string (2nd argument of preg_replace) should be:
(?<$1>.+?)
I am getting a result as a return of a laravel console command like
Some text as: 'Nerad'
Now i tried
$regex = '/(?<=\bSome text as:\s)(?:[\w-]+)/is';
preg_match_all( $regex, $d, $matches );
but its returning empty.
my guess is something is wrong with single quotes, for this i need to change the regex..
Any guess?
Note that you get no match because the ' before Nerad is not matched, nor checked with the lookbehind.
If you need to check the context, but avoid including it into the match, in PHP regex, it can be done with a \K match reset operator:
$regex = '/\bSome text as:\s*'\K[\w-]+/i';
See the regex demo
The output array structure will be cleaner than when using a capturing group and you may check for unknown width context (lookbehind patterns are fixed width in PHP PCRE regex):
$re = '/\bSome text as:\s*\'\K[\w-]+/i';
$str = "Some text as: 'Nerad'";
if (preg_match($re, $str, $match)) {
echo $match[0];
} // => Nerad
See the PHP demo
Just come from the back and capture the word in a group. The Group 1, will have the required string.
/:\s*'(\w+)'$/
I have used two testers of regex, http://www.pagecolumn.com/tool/pregtest.htm and http://www.spaweditor.com/scripts/regex/ both return the results I want but when ever I go 'Live' I get no results.
Heres the script I'm trying to get working:
$ptn = "/\\x(..)/";
$str = "\x47\x4c\x4f\x42\x41\x4c\x53";
preg_match_all($ptn, $str, $matches, PREG_PATTERN_ORDER);
print_r($matches);
Anyone know where I'm going wrong?
PHP converts your $str string into its actual characters, you are actually testing the string "GLOBALS".
If you don't want escape sequences to be parsed, use single quotes:
$ptn = '/\\\x([0-9a-f]{2})/i';
$str = '\x47\x4c\x4f\x42\x41\x4c\x53';
I will get this kind of string from the $_POST array:
$string = "\"Search Text\"";
OR
$string = '\'Search Text\'';
How I will check whether the Search Text included in double quotes or single quotes using regular expression.
Base on Kolink Answer I did like this
echo $subject = "'Search Text'";
$pattern = "/['\"](?=;$)/";
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
Its not giving any result. :(
So, to clarify, your user will be sending something like $string = "Search Text"; in the textbox? Or is that just a part of your server-side code?
If it's the first, you can just search for /['"](?=;$)/ and that will tell you if single or double quotes were used.
If it's the second, your question makes no sense since the quotes are not a part of the string.
I have a URL like this:
http://Example.com/mobile-ds-cams/mobile-gg-cams/ddd-webcams
Example:
$pattern = '/http://Example.com/(\w+)/(\w+)/(\w+)/i';
$replacement="http://Example.com/$2/$3";
$appUrl= preg_replace($pattern, $replacement, $appUrl);
What I want to achieve is this
http://Example.com/mobile-gg-cams/ddd-webcams
I am trying to keep 2 "sub-URLs" instead of 3. but it doesn't work..why?
You need to escape your forward-slashes within the pattern, or use different pattern delimiters.
$pattern = '/http:\/\/Example\.com\/(\w+)\/(\w+)\/(\w+)/i';
$pattern = '#http://Example\.com/(\w+)/(\w+)/(\w+)#i';
It doesn't work correctly because your expression contains characters with special meaning in a regex that have not been properly quoted.
To be 100% certain, use preg_quote like this:
$url = 'http://Example.com/'
$pattern = preg_quote($url.'{word}/{word}/{word}', '/');
$pattern = str_replace($pattern, '{word}', '(\w+)');
$pattern = "/$pattern/i";
$replacement = $url.'$2/$3';
$appUrl= preg_replace($pattern, $replacement, $appUrl);
Otherwise, it's simply too easy to get things wrong. For example, all of the other answers currently posted here get it wrong because they do not properly escape the . in Example.com. You can test this yourself if you feed them a string like e.g. http://Example!com, where ! can be any character you like.
Additionally, you are using strings such as $2 inside a double-quoted string literal, which is not a good idea in PHP because IMHO it's easy to get carried away. Better make that singly quoted and be safe.
Escape the slashes like this:
$pattern = '/http:\/\/Example.com\/(\w+)\/(\w+)\/(\w+)/i';
$replacement="http://Example.com/$2/$3";
$appUrl= preg_replace($pattern, $replacement, $appUrl);