how to highlight last 3 number in the string:
ab9c5lek94ke72koe8nsk9
i want output:
ab9c5lek94ke72koe8nsk9
i tried following:
$str = "ab9c5lek94ke72koe8nsk9";
$numbers = preg_replace('/[^0-9]/', '', $str );
$last3 = substr($numbers, -3);
$highlights = str_split($last3);
$output = preg_replace('/'.implode('|', $highlights).'/i', '<b>$0</b>', $str);
but it highlight:
ab9c5lek94ke72koe8nsk9
You can achieve that easily using regular expression with PHP's preg_replace() function. Just find last 3 digits 3 times at the end of the string. See the following code:
$str1 = 'ab9c5lek94ke2koe8nsk9';
$str2 = 'dag2vue41a89au76zhz30';
echo preg_replace('/(\d)([^\d]*)(\d)([^\d]*)(\d)([^\d]*)$/mui', '<b>$1</b>$2<b>$3</b>$4<b>$5</b>$6', $str1);
echo preg_replace('/(\d)([^\d]*)(\d)([^\d]*)(\d)([^\d]*)$/mui', '<b>$1</b>$2<b>$3</b>$4<b>$5</b>$6', $str2);
Outputs
ab9c5lek94ke2koe8nsk9
and
dag2vue41a89au76zhz30
You can have a regex like below:
/(\d)([^\d]*)(\d)([^\d]*)(\d)([^\d]*)$/
which is basically (\d)([^\d]*) 3 times with a $ sign at the end. It means we are matching a digit followed by 0 or more non digit characters. Note that we do need the $ sign at the end to match only last 3 digits.
Snippet:
<?php
$str = "ab9c5lek94ke2koe8nsk9";
echo preg_replace('/(\d)([^\d]*)(\d)([^\d]*)(\d)([^\d]*)$/',"<b>$1</b>$2<b>$3</b>$4<b>$5</b>$6",$str);
Demo: https://3v4l.org/PoYCG
The replacement string is just having the bold tags with group number of the matched strings which are to be highlighted.
You can split the string on the 3rd from last number, then highlight everything in that sub string and then recombine the substrings.
In my example, I get all of the numbers in the string and put them in array. Then I use that array to get the delimiter (72 in your example), then use that to split the string into two arrays, at that point you can highlight everything in array[1] and then combine it back into array[0]
$str = "ab9c5lek94ke72koe8nsk9";
// get all of the numbers in order and place into it's own array
$numbers = trim(preg_replace('/[^0-9]/', ' ', $str ));
$numbers = preg_split('/\s+/', $numbers);
// using that get the delimiter to where to split the string. and split.
$delim = $numbers[count($numbers)-3];
$arr = explode($delim, $str);
$arr[1] = $delim . $arr[1];
// then highlight everything in $arr[1]
// then combine $arr[0] and $arr[1]
Related
I have a long string like below:
$string = "age1:32,age2:25,age3:52,..."
Now I want to extract the age numbers in this string and then add the numbers together and determine the average age.
I was able to extract the numbers with the help of the following code
$numbers = preg_replace('/[^0-9]/', '', $string );
but the output I get is like this and I cannot add the numbers together.
output:
322552
Using preg_match_all to find all age values followed by array_sum to find the total/average we can try:
$string = "age1:32,age2:25,age3:52,...";
preg_match_all("/age\d+:(\d+)/", $string, $matches);
print_r(array_sum($matches[1]) / count($matches[1])); // 36.333333333333
Restart the fullstring match after each colon, then match one or more digits.
preg_match_all() returns the number of matches ($count).
Divide the sum of the matches by the number of matches.
Code: (Demo)
$string = "age1:32,age2:25,age3:52,...";
$count = preg_match_all('/:\K\d+/', $string, $m);
echo array_sum($m[0] ?? []) / $count;
// 36.333333333333
If you wanted to grab all numbers, just use \d+. (Demo)
$string = "available12available15available22available11...";
$count = preg_match_all('/\d+/', $string, $m);
echo array_sum($m[0] ?? []) / $count;
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 trying to something like this.
Hiding users except for first 3 characters.
EX)
apple -> app**
google -> goo***
abc12345 ->abc*****
I am currently using php like this:
$string = "abcd1234";
$regex = '/(?<=^(.{3}))(.*)$/';
$replacement = '*';
$changed = preg_replace($regex,$replacement,$string);
echo $changed;
and the result be like:
abc*
But I want to make a replacement to every single character except for first 3 - like:
abc*****
How should I do?
Don't use regex, use substr_replace:
$var = "abcdef";
$charToKeep = 3;
echo strlen($var) > $charToKeep ? substr_replace($var, str_repeat ( '*' , strlen($var) - $charToKeep), $charToKeep) : $var;
Keep in mind that regex are good for matching patterns in string, but there is a lot of functions already designed for string manipulation.
Will output:
abc***
Try this function. You can specify how much chars should be visible and which character should be used as mask:
$string = "abcd1234";
echo hideCharacters($string, 3, "*");
function hideCharacters($string, $visibleCharactersCount, $mask)
{
if(strlen($string) < $visibleCharactersCount)
return $string;
$part = substr($string, 0, $visibleCharactersCount);
return str_pad($part, strlen($string), $mask, STR_PAD_RIGHT);
}
Output:
abc*****
Your regex matches all symbols after the first 3, thus, you replace them with a one hard-coded *.
You can use
'~(^.{3}|(?!^)\G)\K.~'
And replace with *. See the regex demo
This regex matches the first 3 characters (with ^.{3}) or the end of the previous successful match or start of the string (with (?!^)\G), and then omits the characters matched from the match value (with \K) and matches any character but a newline with ..
See IDEONE demo
$re = '~(^.{3}|(?!^)\G)\K.~';
$strs = array("aa","apple", "google", "abc12345", "asdddd");
foreach ($strs as $s) {
$result = preg_replace($re, "*", $s);
echo $result . PHP_EOL;
}
Another possible solution is to concatenate the first three characters with a string of * repeated the correct number of times:
$text = substr($string, 0, 3).str_repeat('*', max(0, strlen($string) - 3));
The usage of max() is needed to avoid str_repeat() issue a warning when it receives a negative argument. This situation happens when the length of $string is less than 3.
I need to have the word count of the following unicode string. Using str_word_count:
$input = 'Hello, chào buổi sáng';
$count = str_word_count($input);
echo $count;
the result is
7
which is aparentley wrong.
How to get the desired result (4)?
$tags = 'Hello, chào buổi sáng';
$word = explode(' ', $tags);
echo count($word);
Here's a demo: http://codepad.org/667Cr1pQ
Here is a quick and dirty regex-based (using Unicode) word counting function:
function mb_count_words($string) {
preg_match_all('/[\pL\pN\pPd]+/u', $string, $matches);
return count($matches[0]);
}
A "word" is anything that contains one or more of:
Any alphabetic letter
Any digit
Any hyphen/dash
This would mean that the following contains 5 "words" (4 normal, 1 hyphenated):
echo mb_count_words('Hello, chào buổi sáng, chào-sáng');
Now, this function is not well suited for very large texts; though it should be able to handle most of what counts as a block of text on the internet. This is because preg_match_all needs to build and populate a big array only to throw it away once counted (it is very inefficient). A more efficient way of counting would be to go through the text character by character, identifying unicode whitespace sequences, and incrementing an auxiliary variable. It would not be that difficult, but it is tedious and takes time.
You may use this function to count unicode words in given string:
function count_unicode_words( $unicode_string ){
// First remove all the punctuation marks & digits
$unicode_string = preg_replace('/[[:punct:][:digit:]]/', '', $unicode_string);
// Now replace all the whitespaces (tabs, new lines, multiple spaces) by single space
$unicode_string = preg_replace('/[[:space:]]/', ' ', $unicode_string);
// The words are now separated by single spaces and can be splitted to an array
// I have included \n\r\t here as well, but only space will also suffice
$words_array = preg_split( "/[\n\r\t ]+/", $unicode_string, 0, PREG_SPLIT_NO_EMPTY );
// Now we can get the word count by counting array elments
return count($words_array);
}
All credits go to the author.
I'm using this code to count word. You can try this
$s = 'Hello, chào buổi sáng';
$s1 = array_map('trim', explode(' ', $s));
$s2 = array_filter($s1, function($value) { return $value !== ''; });
echo count($s2);
i have string like
cream 100G
sup 5mg Children
i want to split it before the first occurrence of a digit. so the result should be
array(
array('cream','100G'),
array('sup','5mg Children')
);
can so one tell me how to create pattern for this ?
i tried
list($before, $after) = array_filter(array_map('trim',
preg_split('/\b(\d+)\b/', $t->formula)), 'strlen');
but something went wrong.
Try this:
<?php
$first_string = "abc2 2mg";
print_r( preg_split('/(?=\d)/', $first_string, 2));
?>
Will output:
Array ( [0] => abc [1] => 2 2mg )
The regular expression solution would be to call preg_split as
preg_split('/(?=\d)/', $t->formula, 2)
The main point here is that you do not consume the digit used as the split delimiter by using positive lookahead instead of capturing it (so that it remains in $after) and that we ensure the split produces no more than two pieces by using the third argument.
You don't need regular expressions for that:
$str = 'cream 100g';
$p = strcspn($str, '0123456789');
$before = substr($str, 0, $p);
$after = substr($str, $p);
echo "before: $before, after: $after";
See also: strcspn()
Returns the length of the initial segment of $str which does not contain any of the characters in '0123456789', aka digits.