I have a string sequence that goes like this: <x:object*10/>.
I want to replace that tag with something else that depends on what number it is.
I match the tag above with /<x:object\*[0-9]+\/>/ and using preg_replace i can replace it all with the content I want. I just need that number now.
How can I get the number please?
By capturing it:
/<x:object\*(?P<my_number>[0-9]+)\/>/
It will be captured with name "my_number"...
Full code would be sth like this:
<?php
preg_match_all(`/<x:object\*(?P<my_number>[0-9]+)\/>/`, $where_to_search, $matches);
var_dump($matches); // Dump the matches to see in detail.
?>
Try this one :)
/<x:object\*([0-9]+)\/>/
better use
/<x:object\*([0-9]+)\/>/
Related
I have a big string like this:
[/az_column_text][/vc_column_inner][vc_column_inner width="3/4"]
[az_latest_posts post_layout="listed-layout" post_columns_count="2clm" post_categories="assemblea-soci-2015"]
[/vc_column_inner][/vc_row_inner][/vc_column]
What I need to extract:
assemblea-soci-2015
Of course this value can change, and also the big string can change too. I need a regex or something else to extract this value (it will be always from post_categories="my-value-to-extract") from this big string.
I think to take post_categories=" as the beginning of a possible substring and the next char " as the end of my portion, but no idea how to do this.
Is there an elegant way to do this also for future values with, of course, different length?
You can use this regex in PHP:
post_categories="\K[^"]+
RegEx Demo
You can use this regex:
(?<=post_categories=")[^"]+(?=")
?<= (lookbehind) looks for post_categories=" before the desired match, and (?=) (lookahead) looks for " after the desired match.
[^"] gets the match (which is assumed not to contain any ")
Demo
Example PHP code:
$text='[/az_column_text][/vc_column_inner][vc_column_inner width="3/4"]
[az_latest_posts post_layout="listed-layout" post_columns_count="2clm" post_categories="assemblea-soci-2015"]
[/vc_column_inner][/vc_row_inner][/vc_column]';
preg_match ("/(?<=post_categories=\")[^\"]+(?=\")/", $text,$matches);
echo $matches[0];
Output:
assemblea-soci-2015
This should extract what you want.
preg_match ("/post_categories=\"(.*)\"\[\]/", $text_you_want_to_use)
Let's say I have string like this:
Village_name(315|431 K64)
What I want to do is when I paste that into let's say text box, and click a button, all I will be left with is 315|431.
Is there a way of doing this?
Use the below regex and then replace the match with \1.
(\d+\|\d+)|.
It captures the number|number part and matches all the remaining chars. By replacing all the matched chars with \1 will give you the number|number part only.
DEMO
In php, you may use this also.
(?:\d+\|\d+)(*SKIP)(*F)|.
substring which was matched by \d+\|\d+ regex would be matched first and the following (*SKIP)(*F) makes the regex to fail. Now thw . after the pipe symbol would match all the chars except number|number because we already skipped that part.
DEMO
I know this question has been answered and the answer has been accepted. But I still want to suggest this answer, as you really don't need to use PHP to realize your requirement. Just use Javascript. Its enough:
var str = 'Village_name(315|431 K64)';
var pattern = /\((\w+\|\w+) /;
var res = str.match(pattern);
document.write(res[1]);
Please try this:-
<?php
$str = 'Village_name(315|431 K64)';
preg_match_all('/(?:\d+\|\d+)/', $str, $matches);
echo "<pre/>";print_r($matches);//print in array format completly
$i=0;
foreach($matches as $match){ //iteration through one foreach as you asked
echo $match[$i];
$i++;
}
?>
Output:- http://prntscr.com/74ddg9
Note:- explode can work with some adjustment but if the format only like what you given.So go for preg_match_all. It's best.
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+)
there are a lot of topics like this one but i don't know what the error i tried a lot
so this is the original text
onclick="NewWindow('http://google.com','name','800','600','yes');return false">
this is my code
$re1='(onclick)';
$re2='(=)';
$re3='(.)';
$re4='(NewWindow)';
$re5='(\\()';
$re6='(.)';
$re7='((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))';
$c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7."/is", $txt, $matches);
print_r($matches);
any one can help me to get the url using regular expression and php??
what is the wrong with this code?
Regards
preg_match("/NewWindow\('([^']*)'/",$txt, $matches);
matches[1] contains the url
is it what you need ?
(edit: put in code block because a parenthesis was not escaped correclty
This should work:
preg_match("/onclick=\"NewWindow\('(.*)','n/",$txt,$matches);
I'd use non-greedy matching for this:
preg_match("/onclick=\"NewWindow\('(.*?)'/", $txt, $matches);
Based on your description, the regex I would use, would be:
/(?<=NewWindow\(\').*(http://|https://)[^\'\"]*/i
or
/(?<=onclick=\"NewWindow\(\').*(http://|https://)[^\'\"]*/i
A great tool for testing your regex is: http://gskinner.com/RegExr/
It outputs just the url and only does so if it is preceded by "NewWindow('" in the first example or "onclick="NewWindow('", which means, in your case, 'http://google.com').
I have following string.
?page=1&sort=desc¶m=5&parm2=25
I need to check whether the enter string url is in strict format except variable value.
Like page=, sort=, param=, param2.
Please suggest regular expression.
Thanks
You should use parse_str and check if all the parameters you wanted are set with isset. Regex is not the way to go here.
Maybe this :
\?page=\d+&sort=.+¶m=\d+¶m2=\d+
which translates to :
?page= followed by any digit repeated 1 or more times
&sort= followed by any character repeated 1 or more times
¶m= followed by any digit repeated 1 or more times
¶m2= followed by any digit repeated 1 or more times
I think Alin Purcaru 's suggestion is better
EDIT:
(\?|&)(page=[^&]+|sort=[^&]+|param=[^&]+|parm2=[^&]+)
This way the order doesn't matter
If you care about the order of parameters, something like this:
\?page=[^&]+&sort=[^&]+param=[^&]+param2=[^&]+$
But Alin Purcaru is right - use the parse_str function already written to do this
The regex would be ^\?([\w\d]+=[\w\d]+(|&))*$ As long as your values are Alpha Numeric, but maybe you wanna take a look in to filters if you want to validate an url http://www.php.net/manual/en/book.filter.php
You could use the following regx /\?page=[^&]*sort=[^&]*param=[^&]*param2=/` to match:
if (preg_match("/\?page=([^&]*)sort=([^&]*)param=([^&]*)param2=([^&]*)/i", $inputstr, $matches))
{
echo "Matches:";
print_r($matches); // matches will contain the params
}
else
echo "Params nor found, or in wrong order;