Need some advice with regexp (php) - php

I have a string like this:
('Afghanistan','3',''),(' Albania','1','90 days'),('
Algeria','3',''),(' Andorra','3',''),(' Angola','3','')... etc
I need to select spaces after: ),('
Can someone help me plz?

Why regexp? Just use str_replace("),(' ","),('", $myString);
edit: ),(' was per your request. But I advise you to only look for (', because the very first entry might also contain a space, but isn't caught with the string you requested.
So use str_replace("(' ","('", $myString);

Assuming you have described your input perfectly, this should do the trick.
(?<='\),\(') *

You can use this :
$pattern = "~(?<=\Q),('\E) ~";
all between \Q and \E are seen as literals.
(?<=.....) means preceded by

Just try with following regex:
/\),\('( )/

Related

preg_replace on everything prior to character

I am trying to remove parts of a string which has an ID and : before it. So for example:
2846:ZE1,2847:ZE2,2848:ZE3,713:DY10,412:CF10
But I want it to look like this:
ZE1,ZE2,ZE3,DY10,CF10
I have tried the following preg_replace:
$remove = preg_replace('/[0-9]\:+/', '', $postcodes_id);
But this only removes the last digit and not all of it:
284ZE1,284ZE2,284ZE3,71DY10,41CF10
any help would be great?
A non regex solution
parse_str(str_replace(array(':',','),array('=','&'),$str1),$str1);
Demo
Try this:
$remove = preg_replace('/[0-9]+\:/', '', $postcodes_id);
Adding the + means "one or more digit" instead of your code which is "just one".
I'm pretty sure you don't need the \ before the :...
You have the + in the wrong place, it should be:
preg_replace('/[0-9]+:/', '', $postcodes_id);
You also don't need to escape :, it has no special meaning in regular expressions.

preg_match for select string like #__james_name

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);

Regex To Break Up camelCase String PHP

Let say I have the following string:
getPasswordLastChangedDatetime
How would I be able to split that up by capital letters so that I would be able to get:
get
Password
Last
Changed
Datetime
If you only care about ASCII characters:
$parts = preg_split("/(?=[A-Z])/", $str);
DEMO
The (?= ..) construct is called lookahead [docs].
This works if the parts only contain a capital character at the beginning. It gets more complicated if you have things like getHTMLString. This could be matched by:
$parts = preg_split("/((?<=[a-z])(?=[A-Z])|(?=[A-Z][a-z]))/", $str);
DEMO
Asked this a little too soon, found this:
preg_replace('/(?!^)[[:upper:]]/',' \0',$test);
For instance:
(?:^|\p{Lu})\P{Lu}*
No need to over complicated solution. This does it
preg_replace('/([A-Z])/',"\n".'$1',$string);
This doens't take care of acronyms of course
Use this: [a-z]+|[A-Z][a-z]* or \p{Ll}+|\p{Lu}\p{Ll}*
preg_split("/(?<=[a-z])(?=[A-Z])/",$password));
preg_split('#(?=[A-Z])#', 'asAs')

Exact string replacement in php

I'm looking for a way to replace a string in php that exactly matches with the subject.
For example I got a file named 'hello-world.txt' having three lines:
'http://www.example.com/'
'http://www.example.com/category/'
'http://www.example.com/tag/name/'
and I need to replace the 'http://www.example.com/' with 'http://www.example2.com'
$string=file_get_contents('hello-world.txt');
$string=str_replace('http://www.example.com/','http://www.example2.com',$string);
echo $string;
I will be getting a result similar to this:
'http://www.example2.com/'
'http://www.example2.com/category/'
'http://www.example2.com/tag/name/'
But What I actually need is something like this:
'http://www.example2.com/'
'http://www.example.com/category/'
'http://www.example.com/tag/name/'
Please Help!!!!
You can use preg_replace with the m modifier as:
$string=preg_replace('~^http://www\.example\.com/$~m','http://www.example2.com',$string);
Code In Action
First check if the current line is what you're looking for. If not, just spit it back out.
Either $string=str_replace("'http://www.example.com/'", "'http://www.example2.com'", $string); since in your example you have single quotes around each line or use preg_replace like this:
$string=preg_replace('/^http:\/\/www\.example\.com\/$/', 'http://www.example2.com/', $string);
... if those single quotes aren't supposed to be there. The $ at the end of the regex means the end of the line and the ^ means the beginning of the line. Periods and / need to be escaped hence the \. and \/
I haven't tested this code. Here is a link to preg_replace() http://php.net/manual/en/function.preg-replace.php

How to write regex to find one directory in a URL?

Here is the subject:
http://www.mysite.com/files/get/937IPiztQG/the-blah-blah-text-i-dont-need.mov
What I need using regex is only the bit before the last / (including that last / too)
The 937IPiztQG string may change; it will contain a-z A-Z 0-9 - _
Here's what I tried:
$code = strstr($url, '/http:\/\/www\.mysite\.com\/files\/get\/([A-Za-z0-9]+)./');
EDIT: I need to use regex because I don't actually know the URL. I have string like this...
a song
more text
oh and here goes some more blah blah
I need it to read that string and cut off filename part of the URLs.
You really don't need a regexp here. Here is a simple solution:
echo basename(dirname('http://www.mysite.com/files/get/937IPiztQG/the-blah-blah-text-i-dont-need.mov'));
// echoes "937IPiztQG"
Also, I'd like to quote Jamie Zawinski:
"Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems."
This seems far too simple to use regex. Use something similar to strrpos to look for the last occurrence of the '/' character, and then use substr to trim the string.
/http:\/\/www.mysite.com\/files\/get\/([^/]+)\/
How about something like this? Which should capture anything that's not a /, 1 or more times before a /.
The greediness of regexp will assure this works fine ^.*/
The strstr() function does not use a regular expression for any of its arguments it's the wrong function for regex replacement.
Are you thinking of preg_replace()?
But a function like basename() would be more appropriate.
Try this
$ok=preg_match('#mysite\.com/files/get/([^/]*)#i',$url,$m);
if($ok) $code=$m[1];
Then give a good read to these pages
http://www.php.net/preg_match
preg_replace
Note
the use of "#" as a delimiter to avoid getting trapped into escaping too many "/"
the "i" flag making match insensitive
(allowing more liberal spellings of the MySite.com domain name)
the $m array of captured results

Categories