I have been trying to figure this out really hard and I cannot came out with a solution ,
I have an arrary of strings which is
"Descripcion 1","Description 2"
and I need to filter by numbers, so I thought maybe I can use preg_match() and find when there is exactly 1 number 1 or two or etc, and do my logic, becouse the string before the number may change, but the number cannot, I have tried using
preg_match(" 1{1}","Description 1")
which is suppossed to return true when finds an space followed by the string "1" exactly one time but returns false.
Maybe some of you have had more experience with regular expressions in php and can help me.
Thank you very much in advance.
You could use strpos instead of preg_match!
foreach($array as $string) {
if(strpos($string, ' 1') !== false) {
//String contains " 1"!!
}
}
This would be much faster then a regular expression.
Or, if the Number has to be at the end of the string:
foreach($array as $string) {
if(substr($string, -2) == ' 1') {
//String ends with " 1"!!
}
}
You forgot the regex delimiters. Use preg_match('/ 1/', ...) instead.
However, you do not need a regex at all if you just want to test if a string is contained within another string! See Lars Ebert's answer.
You might have success using
if (preg_match('/(.*\s[1])/', $var, $array)) {
$descrip = $array[1];
} else {
$descrip = "";
}
I tested the above regex on the 3 separate string values of descripcion 1, thisIsAnother 1, andOneMore 1. Each were found to be true by the expression and were saved into group 1.
The explanation of the regex code is:
() Match the regular expression between the parentheses and capture the match into backreference number 1.
.* Match any single character that is not a line break character between zero and as many times possible (greedy)
\s Match a single whitespace character (space, tab, line break)
[1] Match the character 1
Related
I am newbie in PHP. I want to replace certain characters in a string. My code is in below:
$str="this 'is' a new 'string and i wanna' replace \"in\" \"it here\"";
$find = [
'\'',
'"'
];
$replace = [
['^', '*']
['#', '#']
];
$result = null;
$odd = true;
for ($i=0; $i < strlen($str); $i++) {
if (in_array($str[$i], $find)) {
$key = array_search($str[$i], $find);
$result .= $odd ? $replace[$key][0] : $replace[$key][1];
$odd = !$odd;
} else {
$result .= $str[$i];
}
}
echo $result;
the output of the above code is:
this ^is* a new ^string and i wanna* replace #in# #it here#.
but I want the output to be:
this ^is* a new 'string and i wanna' replace #in# "it here".
That means character will replace for both quotation(left quotation and right quotation- condition is for ' and "). for single quotation, string will not be replaced either if have left or right quotation. it will be replaced for left and right quotation.
Ok, I don't know what all that code is trying to accomplish.
But anyway here is my go at it
$str = "this 'is' a new 'string and i wanna' replace \"in\" \"it here\"";
$str = preg_replace(["/'([^']+)'/",'/"([^"]+)"/'], ["^$1*", "#$1#"], $str, 1);
print_r($str);
You can test it here
Ouptput
this ^is* a new 'string and i wanna' replace #in# "it here"
Using preg_replace and a fairly simple Regular expression, we can replace the quotes. Now the trick here is the fourth parameter of preg_replace is $count And is defined as this:
count If specified, this variable will be filled with the number of replacements done.
Therefore, setting this to 1 limits it to the first match only. In other words it will do $count replacements, or 1 in this case. Now because it's an array of patterns, each pattern is treated separately. So each one is basically treated as a separate operation, and thus each is allowed $count matches, or each get 1 match/replacement.
Now rather or not this fits every use case you have I cannot say, but it's the most straight forward way to do it for the example you provided.
As for the match itself /'([^']+)'/
/ opening and closing "delimiters" for the Expression (its a required thing, although it doesn't have to be /)
' literal match, matches ' one time (the opening quote)
( ... ) capture group (group1) so we can use it in the replacement, as $1
[^']+ character set with a [^ not modifier, match anything not in the set, so anything that is not a ' one or more times, greedy
' literal match, matches ' one time (the ending quote)
The replacement "^$1*"
^ literal, adds this char in
$1 use the contents of the capture group (group1)
* literal, adds the char in
Hope that helps understand how it works.
UPDATE
Ok I think I finally deciphered what you want:
string will be replaced for if any word have left and right quotation. example..'word'..here string will be changed..but 'word...in this case not change or word' also not be changed.
This seems like you are trying to say only "whole" words with no spaces.
So in that case we have to adjust our regular expression like this:
$str = preg_replace(["/'([-\w]+)'/",'/"([-\w]+)"/'], ["^$1*", "#$1#"], $str);
So we removed the limit $count and we changed what is in the character group to be more strict:
[-\w]+ the \w means the working set, or in other words a-zA-Z0-9_ then the - is a literal (it has to/should go first in this case)
What we are saying with this is to match only strings that start and end with a quote(single|double) and only if the string within them match the working set plus the hyphen. This does not include the space. This way in the first case, your example, it produces the same result, but if you were to flip it to
//[ORIGINAL] this 'is' a new 'string and i wanna' replace \"in\" \"it here\"
this a new 'string and i wanna' replace 'is' \"it here\" \"in\"
You would get his output
this a new 'string and i wanna' replace ^is* \"it here\" #in#
Before this change you would have gotten
this a new ^string and i wanna* replace 'is' #it here# "in"
In other words it would have only replaced the first occurrence, now it will replace anything between the quotes if and only if it's a whole word.
As a final note you can be even more strict if you only want alpha characters by changing the character set to this [a-zA-Z]+, then it will match only a to z, upper or lower case. Whereas the example above will match 0 to 9 (or any combination of them) the - hyphen, the _ underline and the previously mentioned alpha sets.
Hope that is what you need.
I would like to check if a string is in another string with comma separated. I've write some code and it work like this...
$find_number = '3'
$string_to_search = '1,3,124,12'
preg_match('/[^0-9]'.$find_number.'[^0-9]/', string_to_search);
//match
$find_number = '4'
$string_to_search = '1,3,124,12'
preg_match('/[^0-9]'.$find_number.'[^0-9]/', string_to_search);
//not match
Which is what I expected. The problem is that the first and last string can't recognized in this expression. What did I do wrong?
You don't need regular expressions for such a simple task. If $find_number doesn't contain the separator then you can enclose both $find_number and $string_to_search in separators and use function strpos() to find out if $find_number is present or not in $string_to_search. strpost() is much faster than preg_match().
$find_number = '3';
$string_to_search = '1,3,124,12';
if (strpos(",{$string_to_search},", ",{$find_number},") !== FALSE) {
echo("{$find_number} is present in {$string_to_search}");
} else {
echo("No.");
}
Wrapping $find_number in separators is needed to avoid it finding partial values, wrapping $string_to_search in separators is needed to let it find the $find_number when it is the first or the last entry in $string_to_search.
You need to make sure to check if there are no digits on both sides of the $find_number, and that is why you need (?<!\d) / (?!\d) lookarounds that do not consume text before and after the number you need to match allowing to check the first and last items. The [^0-9] in your pattern are negated character class instances, that require a character other than a digit before and after the find_number. The lookarounds will just fail the match if there is a digit before ((?<!\d) negative lookbehind) or after (with (?!\d) negative lookahead) the find_number. See below:
$find_number = '3';
$string_to_search = '1,3,124,12';
if (preg_match('/(?<!\d)'.$find_number.'(?!\d)/', $string_to_search)) {
echo "match!";
}
See the PHP demo.
An alternative is to use explode with in_array:
$find_number = '3';
$string_to_search = '1,3,124,12';
if (in_array($find_number, explode(",", $string_to_search))) {
echo "match!";
}
See another PHP demo
I have a question. I need to add a + before every word and see all between quotes as one word.
A have this code
preg_replace("/\w+/", '+\0', $string);
which results in this
+test +demo "+bla +bla2"
But I need
+test +demo +"bla bla2"
Can someone help me :)
And is it possible to not add a + if there is already one? So you don't get ++test
Thanks!
Maybe you can use this regex:
$string = '+test demo between "double quotes" and between \'single quotes\' test';
$result = preg_replace('/\b(?<!\+)\w+|["|\'].+?["|\']/', '+$0', $string);
var_dump($result);
// which will result in:
string '+test +demo +between +"double quotes" +and +between +'single quotes' +test' (length=74)
I've used a 'negative lookbehind' to check for the '+'.
Regex lookahead, lookbehind and atomic groups
I can't test this but could you try it and let me know how it goes?
First the regex: choose from either, a series of letters which may or may not be preceded by a '+', or, a quotation, followed by any number of letters or spaces, which may be preceded by a '+' followed by a quotation.
I would hope this matches all your examples.
We then get all the matches of the regex in your string, store them in the variable "$matches" which is an array. We then loop through this array testing if there is a '+' as the first character. If there is, do nothing, otherwise add one.
We then implode the array into a string, separating the elements by a space.
Note: I believe $matches in created when given as a parameter to preg_match.
$regex = '/[((\+)?[a-zA-z]+)(\"(\+)?[a-zA-Z ]+\")]/';
preg_match($regex, $string, $matches);
foreach($matches as $match)
{
if(substr($match, 0, 1) != "+") $match = "+" + $match;
}
$result = implode($matches, " ");
I have this:
$subject ="bla foo bar, blafoo, blabla, ";
$pattern = '/, {2,}/';
if (preg_match($pattern, $subject)){
echo "true";
}else{
echo "false";
}
I want to test whether comma+space occurs twice or more often in the subject. However, the above returns false. So I'm doing something wrong.
Since you are looking for a literal substring, use substr_count:
if (substr_count($subject, ', ') > 2) {
...
} else {
...
}
If you want to do the same but this time with a regex, use preg_match_all that returns the number of occurrences in the same way.
To repeat several characters/tokens in the pattern itself, you need to group them. You can use a non-capturing group (?:...):
(?:, ){2,}
Without a group, only the last token is repeated. (so the space in your example).
But this pattern doesn't describe your string since there are other character between the commas, you must add them:
(?:, [^,]*){2,}
Let's ignore the fact that if you want to check exactly what you're posting, it'd be more efficient to compare strpos($subject, ', ') != strrpos($subject, ', ').
There are 2 problems with your regexp, the repetition is applied to the space only (not the sequence) plus you leave no room for any other characters, so your regexp currently checks if your subject contains ,<SP><SP>
Fixing both issues would lead to:
var_dump(preg_match('~(, .*){2,}~', $subject));
I have an issue understanding the "+" sign.
Here is my example:
$array = array("123 Main St.");
$match = preg_grep("%^\d{1,5}\s[A-Za-z.]+\s[A-Za-z.]{2,7}$%",$array);
foreach($match as $value) {
echo "<pre>" .$value . "<br>";
}
So basically this totally works, but I don't understand the "+" sign's job. If I remove it, it does not work.
+ in regex is to match 1 or more of preceding group OR pattern.
In your example: [A-Za-z.]+ will match 1 OR more of English alphabets (case insensitive) OR a literal dot.
Read more about regular expressions
+ is equal to {1,} meaning 1 or more
+ means at least one or more occurrences of the expression.
In your case [A-Za-z.]+ means there shall be at least a single alphabet or a dot.