I have a string Trade Card Catalogue 1988 Edition I wish to remove everything apart from 1988.
I could have an array of all letters and do a str_replace and trim, but I wondered if this was a better solution?
$string = 'Trade Card Catalogue 1988 Edition';
$letters = array('a','b','c'....'x','y','z');
$string = str_to_lower($string);
$string = str_replace($letters, '', $string);
$string = trim($string);
Thanks in advance
Regular expression?
So assuming you want the number (and not the 4th word or something like that):
$str = preg_replace('#\D#', '', $str);
\D means every character that is not a digit. The same as [^0-9].
If there could be more numbers but you only want to get a four digit number (a year), this will also work (but obviously fails if you there are several four digit numbers and you want to get a specific one) :
$str = preg_replace('#.*?(\d{4,4}).*#', '\1', $str);
You can actually just pass the entire set of characters to be trimmed as a parameter to trim:
$string = trim($string, 'abc...zABC...Z ' /* don't forget the space */);
Related
I have a string:
3 pk. Ready-Dough White Loaves Included $3.99 - 47500 - 00892, 48101
I want to keep only groups of digits longer than 5 characters, and if possible, any dashes or commas between them.
e.g.
47500-00892,48101
My first step was to strip out groups of digits < 4:
preg_replace('/\d{1,4}/', '', $string);
My thinking was "replace any block of digits from 1 to 4 with nothing", but that doesn't do exactly what I thought. Maybe I'm just missing an operator?
Then I was going to strip out all letters and all punctuation except , and -. In my example I would've been left with a starting - because of it being in a string, but a trim() would've been fine to clean that up.
Any help is appreciated!
Had I been patient for 5 more minutes, I would've found the answer: \b
For some reason, working with digits didn't trigger that I needed to use 'word boundaries'.
$string = preg_replace('/\b\d{1,4}\b/', '', $string);
$string = preg_replace('/[^0-9-,]/', '', $string);
$string = trim($string, ',-');
Since there's no reason to perform a replacement, you can use preg_match_all to take what you want and reduce the result array:
$re = '/\d{5,}(?:(?=\s*([-,])\s*\d{5}))?/';
$str = '3 pk. Ready-Dough White Loaves Included $3.99 - 47500 - 00892, 48101';
if ( preg_match_all($re, $str, $matches, PREG_SET_ORDER) ) {
$result = array_reduce($matches, function ($c,$i) { return $c . implode('', $i); });
echo $result;
}
I have a load of labels which are camel case. Some examples are
whatData
whoData
deliveryDate
importantQuestions
What I am trying to do is this. Any label which has the word Data needs to have this word removed. At the point of the capital letter, I need to provide a space. Finally, everything should be uppercase. I have done the removal of Data and the uppercase by doing this ($data->key is the label)
strtoupper(str_replace('Data', '', $data->key))
The part I am struggling with is adding the spaces between words. So basically the above words should end up like this
WHAT
WHO
DELIVERY DATE
IMPORTANT QUESTIONS
How can I factor in the last part of this?
Thanks
It will add spaces before every capital letters. Try this:
$String = 'whatData';
$Words = preg_replace('/(?<!\ )[A-Z]/', ' $0', $String);
Problem
Your regex '~^[A-Z]~' will match only the first capital letter. Check out Meta Characters in the Pattern Syntax for more information.
Your replacement is a newline character '\n' and not a space.
Solution
Use preg_replace(). Try below code.
$string = "whatData";
echo preg_replace('/(?<!\ )[A-Z]/', ' $0', $string);
Output
what Data
Try following:
$string = 'importantQuestions';
$string = strtoupper(ltrim(preg_replace('/[A-Z]/', ' $0', $string)));
echo $string;
This will give you output as:
IMPORTANT QUESTIONS
Try this:
preg_split: split on camel case
array_map: UPPER case all the element
implode: Implode the array
str_replace: Replace the `DATE` with empty
trim: trim the white spaces.
Do this simple things:
echo trim(str_replace("DATE", "", implode(" ", array_map("strtoupper", preg_split('/(?=[A-Z])/', 'deliveryDate', -1, PREG_SPLIT_NO_EMPTY))))); // DELIVERY
This is result exactly what you want.
I'm trying to remove all words of less than 3 characters from a string, specifically with RegEx.
The following doesn't work because it is looking for double spaces. I suppose I could convert all spaces to double spaces beforehand and then convert them back after, but that doesn't seem very efficient. Any ideas?
$text='an of and then some an ee halved or or whenever';
$text=preg_replace('# [a-z]{1,2} #',' ',' '.$text.' ');
echo trim($text);
Removing the Short Words
You can use this:
$replaced = preg_replace('~\b[a-z]{1,2}\b\~', '', $yourstring);
In the demo, see the substitutions at the bottom.
Explanation
\b is a word boundary that matches a position where one side is a letter, and the other side is not a letter (for instance a space character, or the beginning of the string)
[a-z]{1,2} matches one or two letters
\b another word boundary
Replace with the empty string.
Option 2: Also Remove Trailing Spaces
If you also want to remove the spaces after the words, we can add \s* at the end of the regex:
$replaced = preg_replace('~\b[a-z]{1,2}\b\s*~', '', $yourstring);
Reference
Word Boundaries
You can use the word boundary tag: \b:
Replace: \b[a-z]{1,2}\b with ''
Use this
preg_replace('/(\b.{1,2}\s)/','',$your_string);
As some solutions worked here, they had a problem with my language's "multichar characters", such as "ch". A simple explode and implode worked for me.
$maxWordLength = 3;
$string = "my super string";
$exploded = explode(" ", $string);
foreach($exploded as $key => $word) {
if(mb_strlen($word) < $maxWordLength) unset($exploded[$key]);
}
$string = implode(" ", $exploded);
echo $string;
// outputs "super string"
To me, it seems that this hack works fine with most PHP versions:
$string2 = preg_replace("/~\b[a-zA-Z0-9]{1,2}\b\~/i", "", trim($string1));
Where [a-zA-Z0-9] are the accepted Char/Number range.
I'm trying to work out a quick and simple way to remove the first two digits and a decimal
from a string, if that is how it is made up.
I am half way there but need help to finish.
So (first is what I start with, 2nd is the result):
xx.yyy = yyy
aaaaa = aaaaa
test.hello = test.hello
a.test.b.x = a.test.b.x
aa.bb.cc = bb.cc
So it only removes 2 digits and a decimal if it exists like that. If it is three digits and a decimal then it isn't removed.
This is where I am so far:
$string = 'xx.hello';
$pattern = '/(2-digits)./i';
$replacement = ''; // remove if matched
echo preg_replace($pattern, $replacement, $string);
?>
This will do letters, digits, and underscores:
preg_replace('/^\w{2}\./', '', $string);
Without numbers or underscores, both upper and lowercase:
preg_replace('/^[a-zA-Z]{2}\./', '', $string);
Assuming by "digits" you mean actual digits (0-9) and by "decimal" a dot:
$string = preg_replace('/^\d{2}\./','',$string);
Try the following:
$pattern = "~^[0-9A-Za-z]{2}\\.~";
It matches two alphanumerical characters at the beginning of the string, followed by a period (.). Notice that the period has been escaped so the period is interpreted as a literal. (Otherwise, the period matches any single character.)
Using php regexp in a simple way, is it possible to modify a string to add a space after commas and periods that follow words but not after a comma or period that is preceded and followed by a number such as 1,000.00?
String,looks like this with an amount of 1,000.00
Needs to be changed to...
String, looks like this with an amount of 1,000.00
This should allow for multiple instances of course... Here is what I am using now but it is causing numbers to return as 1, 000. 00
$punctuation = ',.;:';
$string = preg_replace('/(['.$punctuation.'])[\s]*/', '\1 ', $string);
You could replace '/(?<!\d),|,(?!\d{3})/' with ', '.
Something like:
$str = preg_replace('/(?<!\d),|,(?!\d{3})/', ', ', $str);
I was searching for this regex.
This post really help me, and I improve solution proposed by Qtax.
Here is mine:
$ponctuations = array(','=>', ','\.'=>'. ',';'=>'; ',':'=>': ');
foreach($ponctuations as $ponctuation => $replace){
$string = preg_replace('/(?<!\d)'.$ponctuation.'(?!\s)|'.$ponctuation.'(?!(\d|\s))/', $replace, $string);
}
With this solution, "sentence like: this" will not be changed to "sentence like: this" (whith 2 blanck spaces)
That's all.
Although this is quite old, I was looking for this same question, and after understanding solutions given I have a different answer.
Instead of checking for character before the comma this regex checks the character after the comma and so it can be limited to alphabetic ones. Also this will not create a string with two spaces after a comma.
$punctuation = ',.;:';
$string = preg_replace("/([$punctuation])([a-z])/i",'\1 \2', $string);
Test script can be checked here.