Highlight a sub string from middle of the string with preg_replace - php

I want to make the middle part of my number string bold.
I have a number string :
$nmbr="55113741659856";
I want to highlight 4 numbers in the middle ,from the 6th position
......7416......
and replace them with bold letters
<b>7416</b>
My currunt code is failing to do what I want
$nmbr="55113741659856";
preg_replace("/d+([0-9]{4,6})/i","<b>$1</b>",$nmbr);
Your Help is much appriciated.
Thanks.

I want to highlight 4 numbers in the middle ,from the 6th position
I'd do:
$nmbr="55113741659856";
preg_replace("/^(\d{5})(\d{4})/","$1<b>$2</b>",$nmbr);

You forgot to add \d+.
preg_replace("/\d+([0-9]{4,6})/i","<b>$1</b>",$nmbr);
The reason is:
\d Find a digit
And you missed the \ here.

Related

Regex to parse the address in one string array element and invoice

Can anyone help me with regular expression in php please .
I want print the first word in the first line and in the second line first word like the text document below
4 newland Avenue. Invoice 0004
London
W3 1lg
Result needs to be something like
"Address" => "4 newland Avenue London W3 1lg",
"Invoice" => "0004"
etc...
I need an expression to escape the invoice number since its in the same line of the first line of the address
I really appreciate your help and thanks in advance
To retrieve the invoice number only, assuming they are always 4 digits and the document is formatted just as you have shown... you can use the following regex.
/\b[Ii]nvoice (\d{4})\b/
In PHP you will likely use preg_match to get the invoice number.
Here is an example: Notice that only the digit is in the capture group and that this regex will ONLY match 4 digits that are preceded by the words "invoice" or "Invoice."

Php preg_replace numbers characters

$my_string = '88888805';
echo preg_replace("/(^.|.$)(*SKIP)(*F)|(.)/","*",$,my_string);
This shows the first and last number like thus 8******5
But how can i show this number like this 888888**. (The last 2 number is hidden)
Thank you!
From this: 8******5
To: 888888**
I'm not sure if you have worked on this Regex pattern to do something unique. However, I will provide you with a general one that should fit your question without using your current pattern.
$my_string = '88888805';
echo preg_replace("/([0-9]+)[0-9]{2}$/","$1**",$,my_string);
Explanation:
The ([0-9]+) will match all digits, this could be replaced with \d+, it's between brackets to be captured as we are going to use it in the results.
[0-9]{2} is going to match the last 2 digits, again, it can be replaced with \d{2}, it's outside the brackets because we don't want to include them in the result. the $ after that is to indicate the end of the test, it's optional anyways.
Results:
Input: 88888805
Output: 888888**
echo preg_replace("/(.{2}$)(*SKIP)(*F)|(.)/","*",$my_string);
If it for a uni assignment, you'd probably want to do this. Basically says, don't match if its the last two characters, otherwise match.

PHP - Find number between 2 Unicode characters

Simple problem but i sux at regular expressions so i need here ur help.
What do i need to type to find a number between two first signs: •
Find out its codes but it doenst help me much: http://www.fileformat.info/info/unicode/char/2022/index.htm
Do you know what should i type in for example preg_match function to make it work?
Example:
• 12345 • TESTTESTTEST
Example Output:
12345
Thanks in advance!
To match a specific Unicode code point, use \x{FFFF} where FFFF is the hexadecimal number of the code point you want to match. You can omit leading zeros in the hexadecimal number between the curly braces. Since \x by itself is not a valid regex token, \x{1234} can never be confused to match \x 1234 times. It always matches the Unicode code point U+1234. \x{1234}{5678} will try to match code point U+1234 exactly 5678 times.
Anyway, what you're probably looking for is something like this:
\x{2022} (\d*) \x{2022}
As for the (\d*) part, it basically means match any digit infinite times, and assign this bit of the pattern as a match (braces stand for capture groups)
Actually i found out a way to do it a bit easier.
I used preg_match() with $pattern = "/[0-9]{1,}/";
Huh xD

Quick PHP regex for digit format

I just spent hours figuring out how to write a regular expression in PHP that I need to only allow the following format of a string to pass:
(any digit)_(any digit)
which would look like:
219211_2
so far I tried a lot of combinations, I think this one was the closest to the solution:
/(\\d+)(_)(\\d+)/
also if there was a way to limit the range of the last number (the one after the underline) to a certain amount of digits (ex. maximal 12 digits), that would be nice.
I am still learning regular expressions, so any help is greatly appreciated, thanks.
The following:
\d+_\d{1,12}(?!\d)
Will match "anywhere in the string". If you need to have it either "at the start", "at the end" or "this is the whole thing", then you will want to modify it with anchors
^\d+_\d{1,12}(?!d) - must be at the start
\d+_\d{1,12}$ - must be at the end
^\d+_\d{1,12}$ - must be the entire string
demo: http://regex101.com/r/jG0eZ7
Explanation:
\d+ - at least one digit
_ - literal underscore
\d{1,12} - between 1 and 12 digits
(?!\d) - followed by "something that is not a digit" (negative lookahead)
The last thing is important otherwise it will match the first 12 and ignore the 13th. If your number happens to be at the end of the string and you used the form I originally had [^\d] it would fail to match in that specific case.
Thanks to #sln for pointing that out.
You don't need double escaping \\d in PHP.
Use this regex:
"/^(\d+)_(\d{1,12})$/"
\d{1,12} will match 1 to 12 digist
Better to use line start/end anchors to avoid matching unexpected input
Try this:
$regex= '~^/(\d+)_(\d+)$~';
$input= '219211_2';
if (preg_match($regex, $input, $result)) {
print_r($result);
}
Just try with following regex:
^(\d+)_(\d{1,12})$

regular expressions multiple statements

I have half my problem working. The problem is: I need to match words that are either 7 letters long and starting with st OR 9 letters long ending with tion. I have code that works for the first half of the question: st\w{5}\s. This will match a 7 letter word such as 'startin' in the example: start startin starting.
However I cant seem to add the second half. (st\w{5}\s)|(tion\w{5}) Does not work in trying to find 'startin' and 'attention' out of: start startin starting attention.
Thanks.
You'll want to look for the word boundaries \b(?:(st\w{5})|(\w{5}tion))\b
use word boundaries, for example:
\b(st[a-z]{5}|[a-z]{5}tion)\b

Categories