Badchip Posted January 3, 2018 Share Posted January 3, 2018 (edited) I'm using "extract_unit" function in order to extract a URL. Now I want to change the first number (15) to 9. http://name15.test.com/live/test/index.m3u8?token=ab8hH3_1Y-d33BdyW_oxcQ&expires=1515075458 (The URL name/number can vary (for example "http://nemo2.test.com...")) I used the function: $unit = preg_replace('~(?:\A..|(?!\A)\G)[^0-9]*\K[0-9]+~', '9', $unit); but it changes all the numbers. Please can you help me with the regular expressions in order to change only the first number? Thank you in advance. Edited January 3, 2018 by Badchip Link to comment Share on other sites More sharing options...
Ingolme Posted January 3, 2018 Share Posted January 3, 2018 If "test.com" is always the same then you can search for that. preg_replace('[0-9]+\\.test\\.com', '9.test.com', $unit); Link to comment Share on other sites More sharing options...
Badchip Posted January 3, 2018 Author Share Posted January 3, 2018 (edited) It doesn't work for me. I get a blank page. It's correct \\.test\\.com ? I tried with: $unit = preg_replace('~[0-9]+~', '9', $unit); ... but it changes the whole URL... (all the numbers to 9) Any idea? Edited January 3, 2018 by Badchip Link to comment Share on other sites More sharing options...
Ingolme Posted January 3, 2018 Share Posted January 3, 2018 Yes, two slashes are necessary, since one of them is to escape the backslash in the PHP string. If the page is blank that means the PHP syntax is incorrect, check your server's error log to find out why. It looks like I forgot delimiters around my regular expression, which you should have been able to identify on your own. preg_replace('#[0-9]+\\.test\\.com#', '9.test.com', $unit); Your expression selects any set of digits, that's why it's changing the whole URL. If you want to identify a particular set of digits then you need to find parts of the URL that are surrounding it. Link to comment Share on other sites More sharing options...
Badchip Posted January 4, 2018 Author Share Posted January 4, 2018 (edited) The two hash (#) have solved the problem. Thank you! I have minimized the code: $unit = preg_replace('#[0-9]+\.#', '9.', $unit); Edited January 4, 2018 by Badchip Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now