There is a string
http://www.ccdcdlmcc.sdc.smdc.,ms.cmcsjh?page=2$#$#L$JK#J$LK#J$
I want to replace the page number with a different string "[pageno]" (including square brackets),
so that my final string will be:
http://www.ccdcdlmcc.sdc.smdc.,ms.cmcsjh?page=[pageno]$#$#L$JK#J$LK#J$
I already tried it with explode and str_split($string), but no luck... can it be done by a regular expression?
My main Stringhttp://www.xyz.com/s/ref=sr_pg_2?rh=n%3A117332031%2Cn%3A!117333031%2Cn%3A118457031%2Cn%3A118458031&page=2&bbn=118457031&ie=UTF8&qid=1337146507 is
Here you go:
$str=preg_replace("/(\\?)(page)(=)(\\d+)/","?page=[pageno]",$yourstring);
Here's another way to do it:
<?php
$str = "http://www.ccdcdlmcc.sdc.smdc.,ms.cmcsjh?page=2$#$#L$JK#J$LK#J$";
echo preg_replace('/page=([0-9]+)/', 'page=[pageno]', $str);
?>
I do assume that the page number is a numeric value only. Otherwise you'll have to extend the [0-9] with [0-9a-zA-Z] and perhaps other characters you want to allow.
Related
I want to find all strings looking like [!plugin=tesplugin arg=dfd arg=2!] and put them in array.
Important feature: the string could contain arg=uments or NOT(in some cases). and of course there could be any number of arg's. So the string could look like:
[!plugin=myname!] or [!plugin=whatever1 arg=22!] or even [!plugin=gal-one arg=1 arg=text arg=tx99!]. I need to put them all in $strarray items
Here is what i did...
$inp = "[!plugin=tesplugin arg=dfd!] sometxt [!plugin=second arg=1 arg=2!] 1sd";
preg_match_all('/\[!plugin=[a-z0-9 -_=]*!]/i', $inp, $str);
but $str[0][0] contains:
[!plugin=tesplugin arg=dfd!] sometxt [!plugin=second arg=1 arg=2!]
instead of putting each expression in a new array item..
I think my problem in regex.. but can't find one. Plz help...
The last ] needs to be escaped and the - in the character class needs to be at the start, end, or escaped. As is it is a range of ascii characters between a space and underscore.
\[!plugin=[a-z0-9 \-_=]*!\]
Regex101 Demo: https://regex101.com/r/zV4bO2/1
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)
I need to validate input patterns using preg_match() so that the patterns is like " anyname1,anyname2,anyname3, ".
Note that there is a comma at the end too. Any letter or number is valid between the commas, numbers do not have to appear at the end. e.g "nam1e,Na2me,NNm22," is valid.
I tried ^([A-Za-z0-9] *, *)*[A-Za-z0-9]$ but did no work. I have gone through other posts too but did not get a perfect answer.
Can someone give me an answer for this?
If you just want the actual values without the comma, then you can simply use this:
\w+(?=[,])
http://regex101.com/r/xT6wE4/1
It sounds like you want to validate that the string contains a series of comma separated alpha-numeric substrings, with an optional trailing comma.
In that situation, this should achieve what you want.
$str = "anyname1,anyname2,anyname3,";
$re = '~^([a-z0-9]+,)+$~i';
if (preg_match($re, $str)) {
// String matches the pattern!
}
else {
// Nope!
}
If the value stored in $str contains a trailing space like in your example, and you don't want to use trim() on the value, the following regex will allow for whitespace at the end of $str:
~^([a-z0-9]+,)+\s*$~i
Why use such a complex solution for a simple problem? You can do the same in two steps:
1: trim spaces, line feeds, line returns and comma's:
$line = trim($line," \r\n,");
2: explode on comma's to see all the names:
$array = explode(',',$line);
You're not telling us what you're going to use it for, so I cannot know which format you really need. But my point is that you don't need complex string functions to do simple tasks.
^([a-zA-Z0-9]+,)+$
You can simply do this.See demo.
http://regex101.com/r/yR3mM3/8
I have some php string like below
abc-1987-mp3-songs
xyz-1999-india-mp3-songs
dec-2001-mp3-songs
ryu-2012-freemp3-songs
Now I want these string splited at last found numeric values like below
abc-1987
xyz-1999
dec-2001
ryu-2012
Please help me that which regex can be used to do this. thanks.
Ok, I had a look (do take some time to learn regex - but meanwhile):
$split = (preg_split('/(^.*?[0-9]+)\-?[^0-9]+/', 'foo-xyz-1999-india-mp3-songs', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
echo $split[0];//<--- foo-xyz-1999, just like you wanted
Dumps an array with foo-xyz-1999 as first value, which is what you need. If you want to know what every part of the regex does read it here
The only difference is that, though the whole string becomes its own delimiter, there are two delimiters (the first part, always ending on a series of numbers and the rest of the string, that doesn't contain any more digits)
Use explode insted of regular expression
for example:-
$str="abc-1987-mp3-songs";
$f=explode("-",$str);
echo $final_result=$f[0]."-".$f[1];
or if you want to use reg exp.then
<?php
$str="abc-1987-mp3-songs";
echo $f=preg_replace('/[^0-9]/','', $str);
?>
Above code give you all the numeric digits of your string.
This would match last occurrence of numeric value from given string:
([\w\d-]*-[\d]+)
This is the link: Regex
I have strings in my application that users can send via a form, and they can optionally replace words in that string with replacements that they also specify. For example if one of my users entered this string:
I am a user string and I need to be parsed.
And chose to replace and with foo the resulting string should be:
I am a user string foo I need to be parsed.
I need to somehow find the starting position of what they want to replace, replace it with the word they want and then tie it all together.
Could anyone write this up or at least provide an algorithm? My PHP skills aren't really up to the task :(
Thanks. :)
$result = preg_replace('/\band\b/i', 'foo', $subject);
will find all occurences of and where it's a word on its own and replace it with foo. \b ensures that there is a word boundary before and after and.
use preg_replace. You don't need to think so hard about this though you will have to learn a little bit about regexes. :)
Read up on str_replace, or for more complex replacements on Regular Expressions and preg_replace.
Examples for both:
<?php
$str = 'I am a user string and I need to be parsed.';
echo str_replace( 'and', 'foo', $str ) . "\n";
echo preg_replace( '/and/', 'foo', $str ) . "\n";
?>
In response to the comments of this answer, note that both examples above will replace every occurrence of the search string (and), even when it happens to be within another word.
To take care of that you either have to add the word separators to the str_replace call (see the comment of an example), but this will get quite complicated when you want to take care of all common word separators (space, commas, dots, exclamation marks, question marks etc.).
An easier to way to fix this problem is to use the power of regular expressions and make sure, the actual search string is not found within another word. See Tim Pietzcker's example below for a possible solution.