This may be easy to do but I'm trying to extract only the highlighted area of the url a user will enter into a field. It will always be between the end /
and ?
Note:
To clarify, this is just a simple text input field that is pasted. I don't need to modify the page url.
USER FIELD URL
<?php $url = 'https://example.com/test/5tPa414MNG1cNfGjJs1Jr?si=tKTd7qJQ1Sda1ZUWoA5Q'; ?>
WHAT I NEED TO EXTRACT
https://example.com/test/ 5tPa414MNG1cNfGjJs1Jr
?si=tKTd7qJQ1Sda1ZUWoA5Q
The code block section only.
END RESULT
<?php $extracted = '5tPa414MNG1cNfGjJs1Jr'; ?>
You can try this:
preg_match("/(?<=\/)(\w+)(?=\?)/", $url, $match);
echo $match[0];
You could use regular expressions as said in an other answer, but they consume performances and aren't mandatory in this case. Here, I'm splitting the URL on the '/' character, and I take the last part. Then I pick a substring from the beginning of the last part to the '?'.
<?php
$url = 'https://example.com/test/5tPa414MNG1cNfGjJs1Jr?si=tKTd7qJQ1Sda1ZUWoA5Q';
//Explode the URL on the /
$urlParts = explode('/', $url);
//Get the last part
$lastItem = end($urlParts);
$extracted = substr($lastItem, 0, strpos($lastItem, '?'));
This solution gives the same result as the others, but it doesn't use regular expression.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.