php - search for string in string and replace that - php

i am asking the question which can be answered by a small attempt in googling but i am not finding anything which can do this, so i am asking here. pls dont downvote and close it, it can be useful for others also:
my problem is: i need to look for some portion of string, and find that and replace that. but my problem is that that portion of string is changing everytime, so i need to inject some regexp.
$url = "www.google.com/test=2";
replace the 'test=2' with 'test=1'
$result = "www.google.com/test=1"
the thing is: the slug can have any number between 1 - 20: test=\d{1,20} this is the case. i tried preg_replace, substr_replace but none of them can do this.

I don't see what's wrong with the following /test=\d+/? I think your problem is that you're forgetting the modifiers (in your example: http://codepad.org/ZZNNr1bH, but you have to use modifiers like so: http://codepad.org/9TEkPYJo)
<?php
$url = "www.google.com/test=2";
$result = preg_replace("/test=\d+/", "test=1", $url);
// ^ ^
var_dump($result);
?>
Update:
That said, like Ashley mentioned, \d{1,20} doesn't mean "one to twenty" but rather "any digit character repeated 1 to 20 times".
If you only want digits from 0-20, use the following regex:
/test=([0-9]|1[0-9]|20)/
Basically meaning (a number from 0-9 OR the number 1 FOLLOWED by any number between 0-9 OR the number 20)
It could also be shortened to
/test=(1?\d|20)/
Meaning (1 repeated 1 or 0 times followed by a digit from 0-9 OR the number 20)

Also, \d{1,20} won't match the numbers between 1 and 20, but 0 and 99,999,999,999,999,999,999 which is not exactly what I think you're after.

$url = "www.google.com/test=2";
$replacement = 1;
echo preg_replace('/(.*test)=([1-20])/', "$1=1" ,$url);
If I understand the question correctly, this should do.

$url = "website.com/test=1/asdasd/....";
$check= "test=1";
$newval = "test=2"
if(strstr($url,$check))
$result = preg_replace($check, $newval, $url);
if the value is contained inside the string, then it can be replaced.

Related

PHP Regex Regular Expressions preg_match() only allow digits with commas

I've asked and it was answered but now, after years, it doesn't work.
I've even tried online regex validators. Not sure what is going on.
Version: PHP 7.0.30 on 64Bit OS
The string should only allow digits with commas.
No commas in the beginning or end.
Spaces between commas is ok but I'd rather not allow it.
The following isn't passing
My regex is:
$DateInvoicedIDs = "1031,453,808,387,111,342,962,706,251,442,362,858,950,738,310,288,99,665,1023,30,894,112,132,148,347,895,382,94,766,683,276,1104,658,34,348,235,786,769,2";
$reg = '/[0-9\s]+(,[0-9\s]+)*[0-9]$/';
if ( preg_match($reg, $DateInvoicedIDs) ) {
echo = $DateInvoicedIDs;
} else { echo "false"; }
I'm using preg_match and getting false.
Any idea?
Test your string and pattern # https://regex101.com/r/3TVmOv/1
When that loads, you will see that there is no match highlighted.
Then add a digit to the end of your string and Whalla! This is because (,[0-9\s]+)* is matching the final 2 and [0-9]$ cannot be satisfied because another digit is required.
If I understand your logic/requirements, I think I'd use ~^\d+(?:\s*,\s*\d+)*$~
This improves the validation because it doesn't allow a mixture of digits and spaces between commas like: 2, 3 4 56, 72 I don't think you want spaces in your comma-separated numerical values.
Pattern Demo
Code: (Demo)
$DateInvoicedIDs = "1031,453,808,387,111,342,962,706,251,442,362,858,950,738,310,288,99,665,1023,30,894,112,132,148,347,895,382,94,766,683,276,1104,658,34,348,235,786,769,2";
$reg = '/^\d+(?:\s*,\s*\d+)*$/';
if (preg_match($reg, $DateInvoicedIDs)) {
echo $DateInvoicedIDs;
} else {
echo "false";
}
It is not matching because of the last [0-9] in your regex. The * in (,[0-9\s]+)* is a greedy match which means that it is consuming all commas followed by digits in your string. There is nothing left after to match against the last [0-9].
So you probably want to reduce your regex to '/[0-9\s]+(,[0-9\s]+)*$/.
The last part of your regex [0-9]$ is what's causing it to fail:
[0-9\s]+ is matching the first number only 1031,
(,[0-9\s]+)* is covering everything until ,2 because it's a single number right after a comma which is what it's looking for
Then [0-9]$ is trying to find one more number but it can't
If the last number is a double-digit number, i.e. ,25 instead of 2, then the that second part (,[0-9\s]+)* would be satisfied because it found at least one number and [0-9]$ would match the next number which is 5 (https://regex101.com/r/0XbHsw/1)
Adding ? for that last part would solve the problem: [0-9\s]+(,[0-9\s]+)*[0-9]?$

Find and split a string by the first character that is not 0

I wanted to know how I could split a string based on the first character that is not 0, e.g.
$ID = ABC-000000160810;
I want to split the id so it looks like this:
$split_ID = 160810;
I tried to just get the last 6 digits, however the problem was that the 6 digits might not always be consistent, so just need to split based on the first non-zero. What is the easiest way to achieve this?
Thanks.
Here's a way using a regular expression:
$id = 'ABC-000000160810';
preg_match('/-0*([1-9][0-9]*)/', $id, $matches);
$split_id = $matches[1];
You can use ltrim if you only want to remove leading zeroes.
$ID = ABC-000000160810;
$split_ID = ltrim($str, '0');
Use ltrim to remove leading characters.
$id = 'ABC-00001234';
$numeric = ltrim(mb_substr($id, mb_strpos($id, '-') + 1), '0');
echo $numeric; // 1234
The above requires the mbstring extension to be enabled. If you encounter an error, either enable the extension or use the non-multibyte functions substr and strpos. Probably you should get in the habit of using the mb_ string functions.
This should also work:
const CHAR_MASK = 'a..zA..Z-0';
$id = 'ABC-00001234';
$numeric = ltrim($id, CHAR_MASK);
echo $numeric; // 1234
For your example "ABC-00000016081" you might use a regex that would match the first part up until you encounter not a zero and then use \K to not include the previously consumed characters in the final match.
[^-]+-0+\K[1-9][0-9]+
[^-]+ Match not a - one or more times using a negated character class
- Match literally
0+ Match one or more times a zero (If you want your match without leading zeroes you could use 0*)
\K Resets the starting point of the reported match
[1-9][0-9]* Match your value starting with a digit 1 -9
Test
You can substr off the ABC part and multiply with 1 to make it a number.
$ID = "ABC-000000160810";
Echo substr($ID, 4)*1;

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 Regex to get text between 2 words with numbers

i'm trying to get the string between two words in a entire string:
Ex.:
My string:
...'Total a Facturar 123,061 221,063 26,860161,16080,580310,760 358,297 Recepcionado'...
I'm using
/(?<=Total a Facturar )(.*?) Recepcionado/
I need the highlighted characters (26,860161,16080,580310,760)
and i get 221,061 221,063 26,860161,16080,580310,760 358,297 Recepcionado with my pattern.
The numbers of the string are always different, i need the numbers that are together without a space.
Thanks
EDIT:
Here is the entire string: eval.in/802292
I hope this will be helpful
Regex demo or Regex demo 2
Regex: (?:\d+(?:\,\d+){2,})
For above question you can also use it like this (?:\d+(?:\,\d+){4})
1. (?:\d+) this will match digits one or more.
2. (?:\,\d+){2,} Adding this in expression will match patterns like , and digits {2,} for 2 or more than 2 times.
PHP code: Try this code snippet here
<?php
ini_set('display_errors', 1);
$string = "Total a Facturar 123,061 221,063 26,860161,16080,580310,760 358,297 Recepcionado";
preg_match("#(?:\d+(?:\,\d+){2,})#", $string, $matches);
print_r($matches);

Position in string of 4 digits number begining with "20"

I've got a string:
$string = "Something here 2014 another text here";
I need to detect position of the first 4 digits number that begins with "20".
So the result of the example would be 15th character of the $string.
Since you have commented with code you tried, I now feel comfortable answering your question properly :) Thank you for trying first!
Your attempt:
preg_match('/20\d\d/', "Something here 2014 another text here",
$matches, PREG_OFFSET_CAPTURE);
... is absolutely correct, however as you correctly pointed out, it would also match 20140 (and indeed 12014 would match too).
To fix this behaviour, you can add word boundaries - because numbers count as word characters. Your regex becomes:
'/\b20\d\d\b/'
This will ensure that there are no numbers (or letters, for that matter) immediately before or after your target four-digit number :)
What about...
$needle = "20";
$pos = strpos($string , $needle);
EDIT:
as requested, a way to get the string from this
$date = substr ($string , $pos , 4 ]);

Categories