Cutting string by first number found - 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 :)

Related

Regex look past character

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

Trim all characters before an integer in a string in PHP?

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

Split string at the first non-alphabetic character

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

extract first N digits from string with regex

I'm trying to cleanup some data, that has N digits in the begining of the string and some in the rest of it. I need to extract only that N first digits.
Here's an example string
1410{{data}} est un program56me de lв556Ђ™
122 datadatadata5654df sdfs989
123datadatadata5654df sdfs989
I need as result to get
1410,122,123
How about:
$str = preg_replace('/^(\d+).*$/', "$1", $str);
Try using this regex :
^([0-9]+)?
using the preg_match command.
It'll spot consecutive-digit sequences at the beginning of a string. :-)
Example :
function getInitial($line)
{
$regex = "^([0-9]+)?";
preg_match($regex, $line, $match);
return $match[1];
}
This should do the trick: ^(\d+). It will instruct the regex engine to start from the beginning of the string, match one or more digits and put them in a group. Any other characters will be ignored.
As I understand, you need at least N digits at the start of each line:
preg_match_all("/^(\d{3,})/m", $text, $matches, PREG_SET_ORDER);
print_r($matches);
one possible regex could be '#^(\d+)#' : in addition to the php function preg_match('#^(\d+)#',$string,$param); $param[1] will return those numbers

Attempting to understand handling regular expressions with php

I am trying to make sense of handling regular expression with php. So far my code is:
PHP code:
$string = "This is a 1 2 3 test.";
$pattern = '/^[a-zA-Z0-9\. ]$/';
$match = preg_match($pattern, $string);
echo "string: " . $string . " regex response: " , $match;
Why is $match always returning 0 when I think it should be returning a 1?
[a-zA-Z0-9\. ] means one character which is alphanumeric or "." or " ". You will want to repeat this pattern:
$pattern = '/^[a-zA-Z0-9. ]+$/';
^
"one or more"
Note: you don't need to escape . inside a character group.
Here's what you're pattern is saying:
'/: Start the expressions
^: Beginning of the string
[a-zA-Z0-9\. ]: Any one alphanumeric character, period or space (you should actually be using \s for spaces if your intention is to match any whitespace character).
$: End of the string
/': End the expression
So, an example of a string that would yield a match result is:
$string = 'a'
Of other note, if you're actually trying to get the matches from the result, you'll want to use the third parameter of preg_match:
$numResults = preg_match($pattern, $string, $matches);
You need a quantifier on the end of your character class, such as +, which means match 1 or more times.
Ideone.

Categories