I have string /ro/weather-kyiv-4944/.Need get ro in this string.
I used preg_match php
preg_match('\/(ro)\/', $url, $matches);
result null. Help me pls guys.
Upd: need ro write in variable
I think you are missing your delimiter characters.
Using # or $ as delimiters is useful because you won't have to escape your / slashes.
Try the following:
preg_match('#/(?<language>.*?)/#', '/ro/weather-kyiv-4944/', $matches);
print_r($matches);
Related
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
I'm a regex-noobie, so sorry for this "simple" question:
I've got an URL like following:
http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-146370543.aspx
what I'm going to archieve is getting the number-sequence (aka Job-ID) right before the ".aspx" with preg_replace.
I've already figured out that the regex for finding it could be
(?!.*-).*(?=\.)
Now preg_replace needs the opposite of that regular expression. How can I archieve that? Also worth mentioning:
The URL can have multiple numbers in it. I only need the sequence right before ".aspx". Also, there could be some php attributes behind the ".aspx" like "&mobile=true"
Thank you for your answers!
You can use:
$re = '/[^-.]+(?=\.aspx)/i';
preg_match($re, $input, $matches);
//=> 146370543
This will match text not a hyphen and not a dot and that is followed by .aspx using a lookahead (?=\.aspx).
RegEx Demo
You can just use preg_match (you don't need preg_replace, as you don't want to change the original string) and capture the number before the .aspx, which is always at the end, so the simplest way, I could think of is:
<?php
$string = "http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-146370543.aspx";
$regex = '/([0-9]+)\.aspx$/';
preg_match($regex, $string, $results);
print $results[1];
?>
A short explanation:
$result contains an array of results; as the whole string, that is searched for is the complete regex, the first element contains this match, so it would be 146370543.aspx in this example. The second element contains the group captured by using the parentheeses around [0-9]+.
You can get the opposite by using this regex:
(\D*)\d+(.*)
Working demo
MATCH 1
1. [0-100] `http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-`
2. [109-114] `.aspx`
Even if you just want the number for that url you can use this regex:
(\d+)
I'm trying to make a regex that will match the argument for a function in PHP. The only problem is the regex doesn't work, or I'm not escaping it well, since I need both double quotes and single quotes to be matched, depending on what the developer used in code.
fn_name("string");
fn_name('string');
I've made two expressions, one for each case, but I guess that can be done way better.
/fn_name\("(.*?)\"\)/
/fn_name\('(.*?)\'\)/
How do I make that one regex, and escape it properly from this?
preg_match_all('/fn_name\("(.*?)\"\)/', file_get_contents($filename), $out);
Thanks.
Use ["'] and backreference (\1):
preg_match_all('/fn_name\((["\'])(.*?)\1\)/', "fn_name('string');", $out);
preg_match_all('/fn_name\((["\'])(.*?)\1\)/', 'fn_name("string");', $out);
See demo.
Try this:
<?php
$s = <<<END
fn_name("string"); fn_name("same line");
fn_name( "str\"ing" );
fn_name("'str'ing'");
fn_name ('string');
fn_name('str\'ing' );
fn_name( '"st"ring"');
fn_name("string'); # INVALID
fn_name('string"); # INVALID
END;
preg_match_all('~fn_name\s*\(\s*([\'"])(.+?)\1\s*\)~', $s, $match, PREG_SET_ORDER);
print_r($match);
Regex explanation
PHP demo
Using preg_match_all(), I want to match something like:
...randomtext...>MATCH1</a>" (MATCH2)"...randomtext... EDIT: to clarify, this is exactly the string I'm trying to extract data from, including the brackets, quotes, angle-brackets etc.
Here's what I've tried: preg_match_all("/^>(.+?)</a>\" \((.+?)\)\"$/", $htmlfile, $matches);
It should extract MATCH1 as $matches[1][0] and MATCH2 as $matches[2][0]
Any idea why it isn't working?
Thanks
You didn't escape your end tag </a>
This should work:
preg_match_all("/>(.+?)<\/a>\" \((.*?)\)/", $htmlfile, $matches);
See Codepad example.
You need to escape the / in your pattern, and you don't want your pattern anchored to ^ and $
So probably this will work: preg_match_all("/>(.+?)<\/a>\" \((.+?)\)\"/", $htmlfile, $matches);
I'm trying to build out a preg_match_all so I can use the array, but the problem is that I can't refine my regex to work.
An example input would be:
#|First Name|#
This text shouldn't be caught
#|Second Value|#
First Name and Second Value should both be in the array.
Here's what I've tried:
preg_match_all('/\#\|(.*?)\|\#\]/',$html, $out);
and
preg_match_all ("/^#\|*\|#*$/i", $html, $out);
The first is about right, it only contains \] near the end which breaks what you want. Using
/\#\|(.*?)\|\#/
does give me correct results, with the matches in $out[1].
Try this regexp :
/\#\|(.*?)\|\#/
Try with:
preg_match_all('/#\|(.*?)\|#/', $html, $out);
Escaping # should not be needed. To specify no # between #'s, you might also use
preg_match_all('/#\|([^#]*)\|#/', $html, $out);
a couple of things, you need to add the '.' character in between your bars. This character means match any character, without it you are saying match zero or more '|' characters. you also need to add the m modifier for multiline matching.
/^#\|.*\|#$/im