I have to replace string but it's simple in PHP but my string just like these here i show you.Please any one help me.
$string = "#x93F;#x902;#x91C";
Above string i want to replace it with
#x91C;#x93F;#x902;
But one thing in these string replace. We don't know last word of the $string #x91C; .
Any word comes in last it's place in to front of that string. How can i solve that please any one help me.
Use capturing groups to capture the characters you want. Later you could replace the matched characters with the chars inside the group.
Regex:
^([^;]*);([^;]*);([^;]*);$
Replacement string:
$3;$1;$2;
DEMO
$string = "#x93F;#x902;#x91C;";
echo preg_replace('~^([^;]*);([^;]*);([^;]*);$~', '$3;$1;$2;', $string);
Output:
#x91C;#x93F;#x902;
((?:[^;]+;)*)([^;]+)(?=$)
Replace by $2;$1.
See demo.
http://regex101.com/r/uH3tP3/9
Related
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+)
Lets suppose I have a string:
$text = "This is my string exec001 and this is the rest of the string exec222 and here is even more execSOMEWORD a very long string!"
I want to replace each occurrence of "exec?" in the string with some new text and at the same time I want to store the text that follows "exec" in a separate variable so it can be used when replacing the text.
For example, say I want to replace each occurrence of exec??? with,
< html>???< /html>< div>???< /div>
, so that the resulting string is:
$text2 = "This is my string <html>001</html><div>001</div> and this is the rest of the string <html>222</html><div>222</div> and here is even more <html>SOMEWORD</html><div>SOMEWORD</div> a very long string!"
How can I do this in PHP?
Thanks very much in advance.
Here is a way to go using preg_replace:
$text = "This is my string exec001 and this is the rest of the string exec222 and here is even more execSOMEWORD a very long string!";
$text2 = preg_replace('/\bexec(\S+)/', "<html>$1</html><div>$1</div>", $text);
echo $text2,"\n";
This will replace all occurrences of execwhatever.
\S+ stands for any NON space character.
\b is a word boundary.
You may find more info here.
Output:
This is my string <html>001</html><div>001</div> and this is the rest of the string <html>222</html><div>222</div> and here is even more <html>SOMEWORD</html><div>SOMEWORD</div> a very long string!
Update according to comment
If you want to replace more than one string, just do:
$text2 = preg_replace('/\bexec([^:\s]+):([^:\s]+)/', "<html>$1</html><div>$2</div>", $text);
Where [^:\s] means any character that is not semi-colon or space
exec\S+
This regex works replacing all exec words.See demo.
http://regex101.com/r/qK1pK7/1
I need a regular expression for select some text like #__james_name in PHP
I tried with :
(^#__[a-z]*)*
But I did not succeed.
help please
UPDATE
I tried with :
\#__([a-z]*)_([a-z]*)
How to using this in preg_match ?
Your grouping is a bit wrong, try
^#_(_[a-z]+)*
see it here on Regexr.
^ is the anchor to the start of the string, you don't want to repeat that. I replaced also the * with a + inside the group, so it requires at least one letter.
Now the string has to start with "#_" and then there can be 0 or more parts starting with an underscore followed by one or more (lowercase) letters.
This regex will match:
#_
#__a
#__a_b
#__a_b_ccccc_d_efadsfaksdjh
preg_match('/(^#__[a-z_]*)/', '#__james_name', $matches);
Do like this
$str=preg_replace('/^#__([\w]+)/', '$1', $str);
I have a string that contains variables in the format {namespace:name} I am trying to create a regular expression for finding all of the variables in the string. I have the following so far, but it isn't working:
$str = " {user:fname} and the last name = {user:lname}";
var_dump(preg_match_all("/^\{(\w):(\w)\}/", $str, $matches));
var_dump($matches);
But it isn't finding any of the tags. The variables can have any word for namespace and name, but letters only with no spaces. Any help would be appreciated.
Update
I tried the following also and received no results: "/\{(\w):(\w)\}/"
Remove the anchor ^ from the regex and allow variables with a length of more than one character.
/^\{(\w):(\w)\}/
becomes:
/\{(\w+):(\w+)\}/