after 10 years with php i still S*** in regex , could you please help me converting this
$x_ary=split('&x=',$url);
to the preg_split equivalent ?
Thank you
In most cases you just need to add delimiters:
preg_split('/&x=/',$url)
/ are fine if you do not need them as part of the pattern. And none of the other symbols are meta characters, so don't need escaping.
Take note that in your case you could just use explode instead, since you don't need a regex.
Related
I have seen similar problems but I cant quite get them to work for my scenario.
I am trying to use regex to find an optional wildcard between 2 strings.
Here is an example of the strings that the regex does find correctly: not happy
if the string was:
not very happy
The code must still match both cases.
The code I have is:
/\b(?<=not(*?).)happy\b/
(happy and very would be variables)
Any help pointing me in the right direction would be very much appreciated.
Some tweaking may be required, but this would match up to two words in between "not" and "happy":
/not(?:\s+\w+){0,2}\s+happy/
This ...
/not +([^ ]* +)?happy/
And feel free to replace ? with {0,2} (for example) to match up to 2 words between not and happy.
I just stuck at this and cannot find solution.
I would like to try to transform a string to lower case using preg_replace.
I just cannot create the right regex.
The reason is that normal strtolower does not support unicode characters.
I know that I could use mb_strtolower but this function seems to be quite slow and beside them not everyone has MB support.
Any clue?
Regards,
Radek
EDIT: Ok, thanks alot for your help guys. I think my approach was not quite correct.
I think it would be much better to use this: How do I detect non-ASCII characters in a string? and then respectively use either the strtolower or mb_strtolower if available.
Regex is not able to change characters by itself, it can only change their order and/or add additional characters/delete some of them.
There is preg_replace_callback or /e flag, but they can manipulate only with known functions, and therefore can't do better than strtolower.
If you can't rely on existense of mb_strolower function, you will have to implement it yourself.
You shouldn't use a preg_replace for this because preg_replace is used to match a certain pattern and replace it with something else. Wat you want is to replace every single uppercase character with a lowercase one, so no need to match a pattern.
mb_strtolower would be the way to go, and if you don't have the mb_ functions you'll have to write a function yourself using a lot of str_replace's...
I am no PHP expert. I am looking for the PHP equivalent of isLetter() in Java, but I can't find it. Does it exist?
I need to extract letters from a given string and make them lower case, for example: "Ap.ér4i5T i6f;" should give "apéritif'. So, yes, there are accentuated characters in my strings.
ctype_alpha().
In addition to regex / preg_replace, you can also use strtoupper($string) and strtolower($string), if you need to universally upper-case a string. As Konrad mentioned, preg_replace is probably your best bet though.
http://php.net/manual/en/function.strtoupper.php
http://www.php.net/manual/en/function.strtolower.php
In PHP (and in Java) you wouldn’t use isLetter to implement it, you’d rather replace all characters that aren’t letters using a regular expression:
echo preg_replace('/\P{L}/', '', input);
Loop up the documentation of preg_replace and the regex pattern syntax desciption, in particular the relevant Unicode character classes.
You could probably use the php-slugs source code, with appropriate modifications.
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
I am quite new to the whole Regex thing and tried to do a preg_match_all in PHP which, kind of the results I wanted but the problem is it matches everything after what I actually wanted... like so:
String: This is something <code>Some code here</code> and more
Match from Regex: <code>Some code here</code> and more
Wanted match from Regex: <code>Some code here</code>
Here is my regular expression that I'm using:
/<code>(.*)<\/code>/
I think its something to do with the beginning and ending / delimiters but I'm not entirely sure.
Any help would be appreciated, thanks!
The star is greedy, meaning it will match as much as it can. Use
(.*?)
instead, making it lazy. You can also use negated character classes:
!<code>([^<]*)</code>!
EDIT: Since mvds deleted his/her answer, here's a tip: You can avoid having to escape the slash (/) if you don't use it as a delimiter, like I did above ^ (used ! )
Here's a good resource on regex:
http://www.regular-expressions.info/repeat.html
you want to make the .* be non greedy. If you put a ? after that part of the pattern it will match everything up to the next part of the regex that matches. So make the regex /<code>(.*?)<\/code>/
You need to disable greediness. Use .*? instead, I believe.
I'm not 100% sure how this is even compiling - you need to escape the second / as so:
/<code>(.*)<\/code>/
So that PHP recognises it as a character to match rather than the end of the regular expression (I think it may be compiling as a substitute regex).
im new to regular expressions in php.
I have some data in which some of the values are stored as zero(0).What i want to do is to replace them with '-'. I dont know which value will get zero as my database table gets updated daily thats why i have to place that replace thing on all the data.
$r_val=preg_replace('/(0)/','-',$r_val);
The code im using is replacing all the zeroes that it finds for eg. it is even replacing zero from 104.67,giving the output 1-4.56 which is wrong. i want that data where value is exact zero that must be replaced by '-' not every zero that it encounter.
Can anyone please help!!
Example of the values that $r_val is having :-
10.31,
391.05,
113393,
15.31,
1000 etc.
This depends alot on how your data is formatted inside $r_val, but a good place to start would be to try:
$r_val = preg_replace('/(?<!\.)\b0\b(?!\.)/', '-', $r_val);
Where \b is a 0-length character representing the start or end of a 'word'.
Strange as it may sound, but the Perl regex documentation is actually really good for explaining the regex part of the preg_* functions, since Perl is where the functionality is actually implemented.
Again, it would be more than helpful if you could supply an example of what the $r_val string really looks like.
Note that \b matches at word boundaries, which would also turn a string like "0.75" into "-.75". Not a desirable result, I guess.
Whilst the other answer does work, it seems overly complex to me. I think you need only to use the ^ and $ chars either side of 0.
$r_val = preg_replace('/^0+$/', '-', $r_val);
^ indicates the regex should match from the beginning of the line.
$ indicates the regex should match to the end of the line.
+ means match this pattern 1 or more times
I altered the minus sign to it's html code equivalent too. Paranoid, yes, but we are dealing with numbers after all, so I though throwing a raw minus sign in there might not be the best idea.
Why not just do this?
if ( $r_val == 0 )
$r_val = '-';
You do not need to use a regex for this. In fact, I'd advise against doing so for performance reasons. The operation above is approximately 20x faster than the regex solution.
Also, the PHP manual advises against using regexes for simple replacements:
If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace().
http://us.php.net/manual/en/function.str-replace.php
Hope that helps!