I want to insert a string into another string.
I have youtube links:
http://www.youtube.com/9bZkp7q19f0
and I want to add /embed after the .com so that I can embed them on the fly.
How can I make them look like this?:
http://www.youtube.com/embed/9bZkp7q19f0
$url = str_replace("youtube.com/", "youtube.com/embed/", $url);
You can use "substr_replace" which means you are replacing text within a portion of a string.
Have a look to this, can get to know more about substr_replace,
http://php.net/manual/en/function.substr-replace.php
$link = "http://www.youtube.com/9bZkp7q19f0";
$link = str_replace("youtube.com/", "youtube.com/embed/", $link);
now
$link = "http://www.youtube.com/embed/9bZkp7q19f0";
Related
$url = localhost/project/index.php?letter=0&position=0&bypass=1
How to change position=0 to position=1?
The new $url value will be:
$url = localhost/project/index.php?letter=0&position=1&bypass=1
You can use parse-str and parse-url approach with the help of http-build-query,
$url = "localhost/project/index.php?letter=0&position=0&bypass=1";
// fetching query paramters and save it to output variable
parse_str(parse_url($url,PHP_URL_QUERY),$output);
// changing position value
$output["position"] = 1;
// building back query string
$query = http_build_query($output);
// creating final string
echo parse_url($url,PHP_URL_PATH)."?".$query;
Demo
Output:-
localhost/project/index.php?letter=0&position=1&bypass=1
You have to use the str_replace() function to replace a specific text from string.
$url = str_replace('position=0','position=1',$url);
I am trying to set up a small script that can play youtube videos but thats kinda besides the point.
I have $ytlink which equals www.youtube.com/watch?v=3WAOxKOmR90
But I want to make it become www.youtube.com/embed/3WAOxKOmR90
Currently I have tried
$result = str_replace('https://youtube.com/watch?v=', "https://youtube.com/watch?v=", $ytlink);
But this returns it as standard
I have also tried
preg_replace('/https://youtube.com/watch?v=/, '/https://youtube.com/embed/', $ytlink);
but both of these dont work.
Instead of using ugly regexes, I recommend using parse_url() with parse_str(). This allows you to be flexible in the event that you want to change something or if Youtube decides to change their URL slightly.
$url = 'https://www.youtube.com/watch?v=3WAOxKOmR90';
// Parse the URL into parts
$parsed_url = parse_url($url);
// Get the whole query string
$query = $parsed_url['query'];
// Parse the query string into parts
parse_str($query, $params);
// Get the parameter you want
$v = $params['v'];
// Now re-build the URL how you want
echo $parsed_url['scheme'].'://'.$parsed_url['host'].'/embed/'.$v;
// Outputs: https://www.youtube.com/embed/3WAOxKOmR90
This works:
$ytlink = 'www.youtube.com/watch?v=3WAOxKOmR90';
$result = str_replace('watch?v=', 'embed/', $ytlink);
echo $result;
$url = 'www.youtube.com/watch?v=3WAOxKOmR90';
echo preg_replace('/.*?v=(\w+)/i', 'www.youtube.com/embed/$1', $url);
http://zyx.com/abc.html?style=1876&price=2%2C1000&size=68
http://zyx.com/abc.html?price=2%2C1000&style=1876&size=68
The url can appear in any of the two form:
I want to remove the entire price=2%2C1000& from my url.
I tried this thread. But with no luck
How to do this?
Try like this :
$str = explode("price","http://zyx.com/abc.html?style=1876&price=1%2C1000%2C2%2C1000&size=68");
$removeable_str = explode("&", $str[1]);
unset($removeable_str[0]);
echo $str[0].join("&",$removeable_str);
Try that:
var s = "http://zyx.com/abc.html?style=1876&price=2%2C1000&size=68"
s.split("price=")[1].split("&")[0];
I'm not sure if I'm going about this the correct way. I would like to take a simple link, like this;
https://www.youtube.com/watch?v=examplevideo
and turn it into
<a href= 'https://www.youtube.com/embed/examplevideo' target=_blank><img src='http://img.youtube.com/vi/examplevideo/0.jpg' width='536' border='1'></a>
In the past, I've been able to change links by using a str_replace, which was very straightforward since you would pull out one pattern and just replace it with another. But, in this case, the pattern that's being kept shows up twice in the output. Is a str_replace the right way to go about it?
Here's one simple way to do it...
// $video_url = "https://www.youtube.com/watch?v=examplevideo";
$videoId = str_replace("https://www.youtube.com/watch?v=", "", $video_url);
enter code here
$videoLink = "<a href= 'https://www.youtube.com/embed/$videoId' target=_blank><img src='http://img.youtube.com/vi/$videoId/0.jpg' width='536' border='1'></a>"
Of course, if your URL is more complex (e.g. ?v=abc&t=123) then this won't work, and you would have to parse the URL more like a URL (i.e. not using str_replace).
You can use parse_url() and parse_str() to get the video ID, and then use sprintf() to build the embed code.
I've made a small function:
function getEmbedded($url) {
$parts = parse_url($url);
$parsed = parse_str($parts['query'], $params);
$result = sprintf("<a href= 'https://www.youtube.com/embed/%s'
target=_blank><img src='http://img.youtube.com/vi/%s/0.jpg'
width='536' border='1'></a>", $params['v'],$params['v']);
return $result;
}
Usage:
echo getEmbedded($url);
This is more efficient than using str_replace() and works even when there are additional query parameters in the video URL.
I need to be able to remove a URL from a variable, I'm wondering how i do this.
Example - Say my script returns http://www.example.com/file.php?id=1234 i need to be able to remove the http://www.example.com/file.php?id= bit, just leaving the id number. If anyone can help, it would be great :)
Something like this?
$var = 'http://www.example.com/file.php?id=1234';
$query = parse_url($var, PHP_URL_QUERY);
$query_components = parse_str($query);
$id = $query_components['id'];
You can use regular expressions:
preg_match("/id=(\\d+)/", $url, $matches);
$id = $matches[1];
Just use $id = $_GET['id'];.
See the docs.
And don't forget to validate and sanitize.
The "id" in this case is being sent to your script as a GET variable, therefore you would access it as follows:
$id = $_GET['id'];
If you mean to say that this URL is not yours to control, then you would do this instead:
print_r(parse_url($url)); // Then analyze the output.