How can I get a portion of the string from the beginning until the first non-alphabetic character?
Example strings:
Hello World
Hello&World
Hello5World
I'd like to get the "Hello" part
You need to use the preg_split feature.
$str = 'Hello&World';
$words = preg_split('/[^\w]/',$str);
echo $words[0];
You can access Hello by $words[0], and World by $words[1]
You can use preg_match() for this:
if (preg_match('/^([\w]+)/i', $string, $match)) {
echo "The matched word is {$match[1]}.";
}
Change [\w]+ to [a-z]+ if you do not want to match the 5 or any numeric characters.
Use preg_split. Split string by a regular expression
If you only want the first part, use preg_match:
preg_match('/^[a-z]+/i', $str, $matches);
echo $matches[0];
Here's a demo.
Use preg_split to get first part of alpha.
$array = preg_split('/[^[:alpha:]]+/', 'Hello5World');
echo $array[0];
Related
I have a strings like this.
$str = "-=!#?Bob-Green_Smith";
$str = "-_#!?1241482";
How can I explode them at the first alphanumeric match.
eg:
$str = "-=!#?Bob-Green_Smith";
becomes:
$val[0] = "-=!#?";
$val[1] = "Bob-Green_Smith";
Quick thought some times the string won't contain the initial string of characters,
so I'd need to check if the first character is alphanumeric or not.. otherwise Bob-Green_Smith would get split when he shouldn't.
Thanks
You can use preg_match.
This will match "non word characters" zero or more as first group.
Then the rest as the second.
The output will have three items, the first is the full string, so I use array_shift to remove it.
$str = "-=!#?Bob-Green_Smith";
Preg_match("/(\W*)(.*)/", $str, $val);
Array_shift($val); // remove first item
Var_dump($val);
https://3v4l.org/m2MCg
You can do this like :
$str = "-=!#?1Bob-Green_Smith";
preg_match('~[a-z0-9]~i', $str, $match, PREG_OFFSET_CAPTURE);
echo $bubString = substr($str, $match[0][1]);
I am facing problem with a regular expression.
I have a string like ('A'&'B')
Now I want to convert it to CONCAT('A'&'B') which is simple and I have done using
str_replace("(", "CONCAT(", $subject)
But I want to replace "(" to "CONCAT(" if the string doesn't have prior string "extract_json_value".
So I don't want to replace extract_json_value('A'&'B') to extract_json_valueCONCAT('A'&'B') but it will stay as it is extract_json_value('A'&'B').
You can expand your regex with a negative lookbehind:
(?<!extract_json_value)\(
Here is a regex demo!
You could use strpos to do this.
if (strpos($subject, '(') === 0) {
$subject = str_replace('(', 'CONCAT(', $subject);
}
If your string contains other text you can use preg_replace() and use a word boundary \B for this.
$subject = preg_replace('/\B\(/', 'CONCAT(', $subject);
You can use negative lookbehind in order to match a group not preceded by a string.
First, let's have a regexp matching all strings but those containing "extract_json_value":
(?<!extract_json_value).*
Now, let's use preg_replace
$string = "extract_json_value('A'&'B')";
$pattern = '/^(?<!extract_json_value)(\(.+\))$/';
$replacement = 'CONCAT\1';
echo preg_replace($pattern, $replacement, $string);
// prints out "extract_json_value('A'&'B')"
It works too with
$string = "('A'&'B')";
...
// prints out "CONCAT('A'&'B')"
However, it does not work with
$string = "hello('A'&'B')";
...
// prints out "helloCONCAT('A'&'B')"
So, continue with a preg_replace_callback:
http://php.net/manual/fr/function.preg-replace-callback.php
I have to cut the first part of my string before any number found. For example:
string: "BOBOSZ 27A lok.6" should be cutted to 'BOBOSZ "
string: "aaa 43543" should be cutted to "aaa "
string: "aa2bhs2" should be cutted to "aa"
Im trying with preg_split and explode funcionts but i can't get the right result for now.
Thanks in advance !
You can use this pattern with the preg_match() function:
preg_match('/^[^0-9]+/', $str, $match);
print_r($match);
pattern details:
^ # anchor: start of the string
[^0-9]+ # negated character class: all that is not a digit one or more times
note: you can replace + by * if you consider that an empty string is a valid result.
If you absolutly want to use the preg_split() function, you do:
$result = preg_split('/(?=(?:[^0-9].*)?$)/s', $str);
echo $result[0];
preg_match('#(\w+)\s?\d+#', $string, $match);
You should get $match[1] as I remember :)
I am trying to match fx. this string
"dfsdfsdf 100.200,00"
This is what i got
[0-9\.]+
That returns
100.200
Is there anyway WITH REGEX i can just look paste the dot. So i will get:
100200
How about:
$str = 'dfsdfsdf 100.200,00';
preg_match('/(\d+)\.(\d+)/', $str, $m);
$res = $m[1] . $m[2];
echo $res,"\n";
outout:
100200
What about this:
preg_replace("/^.*?(\d+)\.(\d+).*?$/", '$1$2', "dfsdfsdf 100.200,00");
it will replace the whole string with the matched digits
working example in phpfiddle
I have an alpha numeric string say for example,
abc123bcd , bdfnd567, dfd89ds.
I want to trim all the characters before the first appearance of any integer in the string.
My result should look like,
abc , bdfnd, dfd.
I am thinking of using substr. But not sure how to check for a string before first appearance of an integer.
You can easily remove the characters you don't want with preg_replace [docs] and a regular expression:
$str = preg_replace('#\d.*$#', '', $str);
\d matches a digit and .*$ matches any character until the end of the string.
Learn more about regular expressions: http://www.regular-expressions.info/.
DEMO
A possible non-Regex solution would be:
strcspn — Find length of initial segment not matching mask
substr — Return part of a string
Example:
$string = 'foo1bar';
echo substr($string, 0, strcspn($string, '1234567890')); // gives foo
$string = 'abc123bcd';
preg_replace("/[0-9]/", "", $string);
or
trim($string, '0123456789');
I believe you are looking for this?
$matches = array();
preg_match("/^[a-z]+/", "dfd89ds", $matches);
echo $matches[0]; // returns dfd
You can use a regex for this:
$string = 'abc123bcd';
preg_match('/^[a-zA-Z]*/i', $string, $matches);
var_dump($matches[0]);
will produce:
abc
To remove the +/- sign, you can simply use:
abs($number)
and get the absolute value.
e.g
$abs = abs($signed_integer);