I have a url from where i am fetching value using GET method and i want to replace that value with the 4 digit random number in the string like
This is my main URL:
http://localhost/ab/index.php?id=3345
These are the strings in my table (fetching from database):
http://anyurl/index.php?id=4876&abc=any
http://anyurl/index.php?id=8726&abc=any
http://anyurl/index.php?id=9026&abc=any
So whenever i open the main url the id's of the table should be replaced according to the main url
you can get the id parameter using global GET variable
$id = $_GET["id"]
then you can change the urls in the table according to it
$url = "http://anyurl/index.php?id=".$id."&abc=any"
Hope this will help you
If you want to replace the id with preg_replace in string then you can do like below:
<?php
$string = 'http://anyurl/index.php?id=4876&abc=any';
$new_string = preg_replace('/[0-9]+/', $_GET["id"], $string);
echo $new_string;
// Will display http://anyurl/index.php?id=3345&abc=any
?>
I know that it was asked years ago, but, probably, someone will find my solution helpful
So, I also offer to use preg_replace(), as Amit Gupta, but improve it for cases when you could have other numbers before ID value:
$url = 'http://anyurl/index.php?foo=0713&id=4876&abc=any';
$new_id = $_GET['id'];
// regex: catch 1 or more digits after 'id='
$new_url = preg_replace( '/id=(\d+)/', $new_id, $url );
If $_GET['id'] is 4920, for example, $new_url will be equal to http://anyurl/index.php?foo=0713&id=4920&abc=any
Related
sorry if my question was stupid, please someone help me to fix this issue.
i have string like
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/9/";
this $str_value is dynamic , it will change each page. now i need to replace 9 in this string as 10. add integer 1 and replace
for example if the $str_value = "http://99.99.99.99/var/test/src/158-of-box.html/251/"
then output should be
http://99.99.99.99/var/test/src/158-of-box.html/252/
i tried to replace using preg_match but i m getting wrong please somesone help me
$str = preg_replace('/[\/\d+\/]/', '10',$str_value );
$str = preg_replace('/[\/\d+\/]/', '[\/\d+\/]+1',$str_value );
Thank's for the answer, #Calimero! You've been faster than me, but I would like to post my answer, too ;-)
Another possibilty is to fetch the integer by using a group. So you don't need to trim $matches[0] to remove the slashes.
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/9/";
$str = preg_replace_callback('/\/([\d+])\//', function($matches) {
return '/'.($matches[1]+1).'/';
}, $str_value);
echo $str;
You need to use a callback to increment the value, it cannot be done directly in the regular expression itself, like so :
$lnk= "http://99.99.99.99/var/test/src/158-of-box.html/9/";
$lnk= preg_replace_callback("#/\\d+/#",function($matches){return "/".(trim($matches[0],"/")+1)."/";},$lnk); // http://99.99.99.99/var/test/src/158-of-box.html/10/
Basically, the regexp will capture a pure integer number enclosed by slashes, pass it along to the callback function which will purge the integer value, increment it, then return it for replacement with padded slashes on each side.
I'd suggest also another approach based on explode and implode instead of doing any regexp stuff. In my opinion this is more readable.
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/11/";
// explode the initial value by '/'
$explodedArray = explode('/', $str_value);
// get the position of the page number
$targetIndex = count($explodedArray) - 2;
// increment the value
$explodedArray[$targetIndex]++;
// implode back the original string
$new_str_value = implode('/', $explodedArray);
I got this code:
if(preg_match("/^(?:https?:\/\/)?(?:www\.)?youtu(.be\/|be\.com\/watch\?v=)(\w{11})$/", $url)){
preg_match("/^(?:https?:\/\/)?(?:www\.)?youtu(.be\/|be\.com\/watch\?v=)(\w{11})$/", $url, $matches);
$vid = str_replace(' ', '', $matches[0]);
}
That pretty much checks if the URL is a Youtube video. How do I assign the last 11 characters from the URL to the $vid variable?
Example:
URL: https://www.youtube.com/watch?v=ASDASDASDAS
$vid = ASDASDASDAS
First, you don't want to check for the last 11 characters, because the video ID might be changed any time to a different length. What you should always do is make something flexible enough. So in this case, you should check for ?.*v=([^&]+), since that will get the match until the next &. So your code would look like this:
//make this a variable, since you're using it multiple times.
$re = "/^(?:https?:\/\/)?(?:www\.)?youtu(.be|be\.com)\/watch\?.*?v=([^&#]+).*$/";
if (preg_match($re, $url)){
preg_match($re, $url, $matches);
$vid = $matches[2]; //the 2nd group in the match, so the 2nd set of ()s
}
In that code, video urls that contain other URL parameters will also still work, and it's much more flexible with the length of the video ID.
Demo - look to the right where it says 2., it will mention the video ID there (which is what's put inside $vid).
The parse_str and parse_url functions will do the job ( no need for regular expressions ):
$link = 'https://www.youtube.com/watch?v=ASDASDASDAS';
parse_str( parse_url( $link, PHP_URL_QUERY ), $query );
print_r( $query );
/*
Array
(
[v] => ASDASDASDAS
)
*/
Perhaps something like:
$vid = substr($url,strlen($url)-11);
The straightforward (correct) answers have been made, but I would encourage you to take a look at oembed standard and library for php like https://code.google.com/p/php-oembed/. This would help you get info from almost all image/video services out there, generate embed code and more.
I am trying to create a regular expression to do the following (within a preg_replace)
$str = 'http://www.site.com&ID=1620';
$str = 'http://www.site.com';
How would I write a preg_replace to simply remove the &ID=1620 from the string (taking into account the ID could be variable string length
thanks in advance
You could use...
$str = preg_replace('/[?&;]ID=\d+/', '', $str);
I'm assuming this is meant to be a normal URL, hence the [?&;]. If that's the case, the & should be a ?.
If it's part of a larger list of GET params, you are probably better off using...
parse_str($str, $params);
unset($params['ID']);
$str = http_build_query($params);
I'm guessing that & is not allowed as a character in the ID attribute. In that case, you can use
$result = preg_replace('/&ID=[^&]+/', '', $subject);
or (possibly better, thanks to PaulP.R.O.):
$result = preg_replace('/[?&]ID=[^&]+/', '', $subject);
This will remove &ID= (the second version would also remove ?ID=) plus any amount of characters that follow until the next & or end of string. This approach makes sure that any following attributes will be left alone:
$str = 'http://www.site.com?spam=eggs&ID=1620&foo=bar';
will be changed into
$str = 'http://www.site.com?spam=eggs&foo=bar';
You can just use parse_url
(that is if the URL is of the form: http://something.com?id1=1&id2=2):
$url = parse_url($str);
echo "http://{$url['host]}";
if I have this url: node/95/pdf/1. How will I able to get the numeric/value 1? Tried the parse_url but gave me the wrong output.
PS: the value 1 is just an example, the id is dynamic depends on what the user click.
I would use sscanf
Untested example:
list($node_id, $pdf_id) = sscanf($url, "node/%d/pdf/%d");
$node_id contains the node id, $pdf_id contains the pdf id. According to your comment: Yes, you can output it with e.g. echo $pdf_id;.
If you need them both in an array, you can remove the list() method, doing it like this:
$ids = sscanf($url, "node/%d/pdf/%d");.
This returns an array with both node and pdf id in $ids.
Finally, if you just need the pdf id, you could do
$id = sscanf($url, "node/95/pdf/%d");.
I just showed how to fetch both because I assumed you may need both numbers from your url.
Edit
seeing all the other answers after posting my solution, I am wondering why everyone is solving this with multiple functions when there is a function available that does exactly what he needs: parsing a string according to a format. This also leads to less sql-injection prone code IMHO. And it doesn't break something when the url gets extended or query strings are appended.
Edit 2
list($node_id, $sub, $sub_id) = sscanf($url, "node/%d/%[^/]/%d"); will get you the "pdf" and it's id separate instead of "node/%d/%s/%d". This is because char / is also matched by %s. Using %[^/] matches everything except the forward slash.
You can do this:
$id = end(explode('/', 'node/95/pdf/1'));
Example:
$arr = explode('/', 'node/95/pdf/1');
$id = end($arr);
echo $id; // 1
$url = "node/95/pdf/1";
// Find the last occurence of a slash, get everything after that.
$id = substr($url, strrpos($url, "/") + 1 );
Try with:
$input = 'node/95/pdf/1';
$parts = explode('/', $input);
$output = (int) $parts[3];
This question is more of a "what is the best/easiest way to do"-type-of-question. I would like to grab just the users id from a string such as
User name
I would like to parse the string and get just the "123456" part of it.
I was thinking I could explode the string but then I would get id=123456&blahblahblah and I suppose I would have to somehow dynamically remove the trash from the end. I think this may be possible with regex but I'm fairly new to PHP and so regex is a little above me.
The function parse_str() will help here
$str = "profile.php?rdc332738&id=123456&refid=22";
parse_str($str);
echo $id; //123456
echo $refid; //22
Just grab any character from id= up to & or " (the latter accounts for the case where id is put last on the query string)
$str = 'a href="/profile.php?rdc332738&id=123456&refid=22">User name</a>';
preg_match('/id=([^&"]+)/', $str, $match);
$id = $match[1];
Regex:
.*id[=]([0-9]*).*$
if you explode the string:
$string="User name"
$string_components=explode('&','$string');
The user id part (id=123456) will be:
$user_id_part=$string_components[1];
then you could do a string replace:
$user_id=str_replace('id=','$user_id_part');
And you have the user id