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
Related
I am trying to replace all occurrences of '/ /' in a string with '/'. I can't seem to find the right regex expression to match the string to replace, however.
Currently, I have preg_replace('/\/\s\//', '/', $string); but this does not replace any occurrences of '/ /'.
To be clear, I would like to match any occurrences of a forward slash followed by whitespace followed by a forward slash. It seems that the last forward slash messes things up. I have also tried str_replace(), but to no avail.
EDIT:
The regex expression works correctly if I run it on a string like so:
echo(preg_replace('/\/\s?\//m', '/', ".... . -.-- / / .... ---")); and I get the output: .... . -.-- / .... ---. But when I run it like this:
$morse = preg_replace('/\/\s?\//m', '/', $morse); The replacement does not work. For the record, the ouput of echo($morse) before preg_replace is .... . -.-- / / .... --- and after it is the exact same. I have no idea what could be causing this, some sort of weird encoding in the $morse variable string?
Please help, this is driving me crazy. Thanks in advance!
The regex pattern you have written is correct and it will replace if the / / occurrence is found. I updated your pattern to /\/\s?\//m. You can try the following way.
$re = '/\/\s?\//m';
$str = 'test string // test string / /';
$subst = '/';
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
The use of other delimiters would simplify things enormously here:
$regex = '~/\s+/~';
$replacement = '/';
$string = preg_replace($regex, $replacement, $original_string);
Note, that your original string is left untouched.
How can i remove part of string from example:
##lang_eng_begin##test##lang_eng_end##
##lang_fr_begin##school##lang_fr_end##
##lang_esp_begin##test33##lang_esp_end##
I always want to pull middle of string: test, school, test33. from this string.
I Read about ltrim, substr and other but I had no good ideas how to do this. Becouse each of strings can have other length for example :
'eng', 'fr'
I just want have string from middle between ## and ##. to Maye someone can help me? I tried:
foreach ($article as $art) {
$title = $art->titl = str_replace("##lang_eng_begin##", "", $art->title);
$art->cleanTitle = str_replace("##lang_eng_end##", "", $title);
}
But there
##lang_eng_end##
can be changed to
##lang_ger_end##
in next row so i ahvent idea how to fix that
If your strings are always in this format, an explode way looks easy:
$str = "##lang_eng_begin##test##lang_eng_end## ";
$res = explode("##", $str)[2];
echo $res;
You may use a regex and extract the value in between the non-starting ## and next ##:
$re = "/(?!^)##(.*?)##/";
$str = "##lang_eng_begin##test##lang_eng_end## ";
preg_match($re, $str, $match);
print_r($match[1]);
See the PHP demo. Here, the regex matches a ## that is not at the string start ((?!^)##), then captures into Group 1 any 0+ chars other than newline as few as possible ((.*?)) up to the first ## substring.
Or, replace all ##...## substrings with `preg_replace:
$re = "/##.*?##/";
$str = "##lang_eng_begin##test##lang_eng_end## ";
echo preg_replace($re, "", $str);
See another demo. Here, we just remove all non-overlapping substrings beginning with ##, then having any 0+ chars other than a newline up to the first ##.
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 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);
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];