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.
Related
I have this kind of string:
DURATION : 00:23:55.060000000
I want to convert it to this:
00:23:55.060000000
Please note that after DURATION, it has many spaces.
EDIT:
It seems that I made you upset, guys. :D
I did this and not working:
preg_replace('/^Duration,\s+/', '', $result[20])
How to do it with php?
Your regex is messed up. You are looking for something in uppercase and your regex is in lowercase. And there is a comma laying around.
So if you rewrite that like:
preg_replace('/^Duration\s+: /i', '', $result[20])
(the i modifier after the regular expression says its case insenstive)
or:
preg_replace('/^DURATION\s+: /', '', $result[20])
It'll work.
But mostly, it seems that you want to catch the timestamp, and disregard the rest. For me, the code would be much clearer if your regex reflected that.
E.g.:
if (preg_match("|(?<timestamp>\d\d:\d\d\:\d\d\.\d{9})|", $string, $matches)) {
echo $matches['timestamp'];
}
Solution :
$duration = substr($duration, strpos($duration, (":")) + 2);
I hope this can be useful for others who need it:
preg_replace('/duration|^(.*?):|\s/i', '', $result[20]);
code explanation:
first, strip the duration, and then the first colon : lastly all spaces.
put i at the end to the regex to declare that the search is incase-sensitive.
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:
/\),\('( )/
I am using str_replace() to replace all the slashes in a string with '%' and then I run the newly edited string through a function. One of the things this function does is adds '%%%' to the string.
Now, I need to replace only the percentage signs that I replaced before, that were slashes before, with slashes again.
So if the original string looks like this: 'the/wuick/bornw/foc/jumps/over/the/lazy/dog.'
After passing it through the function it will look like this: 'The%quick%brown%fox%jumps%over%the/lazy%dog.%%%'
And then after putting it through the part I need help with, it will look like this: 'The/quick/brown/fox/jumps/over/the/lazy/dog.%%%'
I would greatly appreciate any and all help replacing only the % that I had done with str_replace() before.
To replace single % signs with slashes, you can use
$result = preg_replace('/(?<!%)%(?!%)/', '/', $subject);
This uses negative lookaround assertions to make sure only those % signs are matched that are neither preceded nor followed by another % sign.
See it on regex101.com.
without too many complications execute in this order:
$str = 'The%quick%brown%fox%jumps%over%the/lazy%dog.%%%';
$str= str_replace('%%%','***triple_percent**', $str);
$str= str_replace('%','/', $str);
$str= str_replace('***triple_percent**','%%%', $str);
Idealy first see why you have so many %, I am sure you can simplify your functions.
Another solution is using regular expressions like Tim says in his answer
$str= preg_replace('/(?<!%)%(?!%)/', '/', $str);
Break apart means:
(?<!%) Not % before
% find %
(?!%) Not % after
also add g so it can find it many times, and i for case sensitive in the case that you might need it:
$str= preg_replace('/(?<!%)%(?!%)/ig', '/', $str);
Add another replate with blank string like:
$string = str_replace('%', '', $string);
I use preg_replace function. I want the function not to remove apostrophe (') character. So I want it to return the word as (o'clock) .
How can I do that?
$last_word = "o'clock.";
$new_word= preg_replace('/[^a-zA-Z0-9 ]/','',$last_word);
echo $new_word;
Try:
$last_word = "o'clock.";
$new_word= preg_replace('/[^a-zA-Z0-9\' ]/','',$last_word);
echo $new_word;
Demo here: http://ideone.com/JMH8F
That regex explicitly removes all characters except for letters and numbers. Note the leading "^". So it does what you ask it to.
So most likely you want to add the "'" (apostrophe) to the exclusion set inside the regex:
'/[^a-zA-Z0-9\' ]/'
Change your original '/[^a-zA-Z0-9 ]/' to "/[^a-zA-Z0-9 ']/". This simply includes the apostrophe in the negated character class.
See an online example.
Aside: my suggestion would be to use double-quotes for the string (as you have with "o'clock.") since mixing backslash escapes with PHP strings and regex patterns can get confusing quickly.
Try this. It may help..
$new_word= preg_replace('/\'/', '', $last_word);
Demo: http://so.viperpad.com/F82z9o
That regex you use does not remove the "'" (apostrophe). Instead it does not match the subject string at all because of the "." (dot). In that case preg_replace() returns NULL.
Here is what I am trying to achieve in PHP:
I have this string: host/%%%25asd%%
Now I want to loop through it and replace only the % _blank characters with %25. So I get the output as host/%25%25%25asd%25%25. (The %25 was untouched because the % wasn't followed by another %)
How should I go by doing this? regex? if so do you have an example? or loop through every character in the string and replace? I was thinking about using str_pos for this but it might after one replacement, the positions in the string would change :(
[Edit: Let me add a couple more information to ease up the confusion. %25 is just an example, it could be anything like %30 or %0a, I won't know before hand. Also the string could also be host/%%25asd%% so a simple replace for %% screw it up as host/%2525asd%25 instead of host/%25%25asd%25. What am trying to achieve is to parse a url into how google wants it for their websafe api. http://code.google.com/apis/safebrowsing/developers_guide_v2.html#Canonicalization. If you look at their weird examples.]
Use preg_replace:
$string = preg_replace('/%(?=%)/', '%25', $string);
Note the lookahead assertion. This matches every % that is followed by a % and replaces it with %25.
Result is:
host/%25%25%25asd%25%
EDIT Missed the case for the last %, see:
$string = preg_replace('/%(?=(%|$))/', '%25', $string);
So the lookahead assertion checks the next character for another % or the end of the line.
How about a simple string (non-regex) replace of '%%' by '%25%25'?
This is assuming you indeed want the output to be host/%25%25%25asd%25%25 as you mentioned and not one %25 at the end.
edit: This is another method that might work in your case:
Use the methods urlencode and urldecode, e.g.:
$string = urlencode(urldecode("host/%%%25asd%%"));
use str_replaceenter link description here instead of preg_replace , its a lot easier to apply and clean
How about something like this?
s/%(?![0-9a-f]+)/%25/ig;
$str = 'host/%%%25asd%%';
$str =~ s/ % (?![0-9a-f]+) /%25/xig;
print $str."\n";
host/%25%25%25asd%25%25