Error using preg_relace to change url youtube? - php

I have a sample code:
<?php
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w';
$pattern = '/http:\/\/www\.youtube\.com\/watch\?(.*?)v=([a-zA-Z0-9_\-]+)(\S*)/i';
$replace = $pattern.'&w=550';
$string = preg_replace($pattern, $replace, $url);
?>
How to result is http://www.youtube.com/watch?v=KTRPVo0d90w&w=550

You can just append using the . operator:
<?php
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w';
$string = $url.'&w=550';
?>

Use preg_match instead:
<?php
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w&s=222';
$pattern = '/v=[^&]+/i';
preg_match($pattern, $url, $match);
echo 'http://www.youtube.com/watch?'.$match[0].'&w=550';
?>

Like below?
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w';
$bit = '&w=550';
echo "${url}${bit}";

Don't get me wrong, I'm not looking to gain any points here, but just thought I would add to this question and include a few options. I love toying with ideas like this every once in a while.
Using jh314's idea to concatenate the strings, thought that this could be used for future use, to actually replace a string inside the video's YouTube number, should the occasion ever present itself.
Such as $number for instance.
<?php
$url = 'http://www.youtube.com/watch?v=';
$number = 'KTRPVo0d90w';
$string = $url.$number.'&w=550';
// Output to screen
echo $string;
echo "<br>";
// Link to video
echo "Click for the video";
?>
The same could easily be done for the video's width.

Related

PHP - find and replace a string between two variables when the length can't be determined

I'm trying to create a simple PHP find and replace system by looking at all of the images in the HTML and add a simple bit of code at the start and end of the image source. The image source has something like this:
<img src="img/image-file.jpg">
and it should become into this:
<img src="{{media url="wysiwyg/image-file.jpg"}}"
The Find
="img/image-file1.jpg"
="img/file-2.png"
="img/image3.jpg"
Replace With
="{{media url="wysiwyg/image-file.jpg"}}"
="{{media url="wysiwyg/file-2.png"}}"
="{{media url="wysiwyg/image3.jpg"}}"
The solution is most likely simple yet from all of the research that I have done. It only works with one string not a variety of unpredictable strings.
Current Progress
$oldMessage = "img/";
$deletedFormat = '{{media url="wysiwyg/';
$str = file_get_contents('Content Slots/Compilied Code.html');
$str = str_replace("$oldMessage", "$deletedFormat",$str);
The bit I'm stuck at is find the " at the end of the source to add the end of the required code "}}"
I don't like to build regular expressions to parse HTML, but it seems that in this case, a regular expression will help you:
$reg = '/=["\']img\/([^"\']*)["\']/';
$src = ['="img/image-file1.jpg"', '="img/file-2.png"', '="img/image3.jpg"'];
foreach ($src as $s) {
$str = preg_replace($reg, '={{media url="wysiwyg/$1"}}', $s);
echo "$str\n";
}
Here you have an example on Ideone.
To make it works with your content:
$content = file_get_contents('Content Slots/Compilied Code.html');
$reg = '/=["\']img\/([^"\']*)["\']/';
$final = preg_replace($reg, '={{media url="wysiwyg/$1"}}', $content);
Here you have an example on Ideone.
In my opinion what you are doing is not the best way this can be done. I would use abstract template for this.
<?php
$content = file_get_contents('Content Slots/Compilied Code.html');
preg_match_all('/=\"img\/(.*?)\"/', $content, $matches);
$finds = $matches[1];
$abstract = '="{{media url="wysiwyg/{filename}"}}"';
$concretes = [];
foreach ($finds as $find) {
$concretes[] = str_replace("{filename}", $find, $abstract);
}
// $conretes[] will now have all matches formed properly...
Edit:
To return full html use this:
<?php
$content = file_get_contents('Content Slots/Compilied Code.html');
preg_match_all('/=\"img\/(.*)\"/', $content, $matches);
$finds = $matches[1];
$abstract = '="{{media url="wysiwyg/{filename}"}}"';
foreach ($finds as $find) {
$content = preg_replace('/=\"img\/(.*)\"/', str_replace("{filename}", $find, $abstract), $content, 1);
}
echo $content;

How can I get this word str_replace by PHP

I want to replace the <a href='http://example.org/'>this word</a> element. But the problem is that "this word" can be any word.
<?php
$link = "http://example.com";
$site = file_get_contents($link);
$ades = "<a href='http://example.org/'>this word</a>";
$bdes = "";
$site = str_replace($ades,$bdes,$site);
echo $site;
?>
'This word' is a variable
'This word' can be pink, blue, door etc.
How can I get it?
edited :
I just want to remove like these codes
blaasdsad
gertvb
ertvvuyrt
awceawce
8k9789k789k
and else
$ades = "<a href='http://example.org/'>this word</a>";
echo strip_tags($ades);
Just use strip_tags function to remove the html tags. The output will be a string with the color name.
More info about strip_tags Here!!!
If your question is how to access every tag of your actual page which has this form <a href='http://example.org/'>any text or word</a>, I would use preg_replace, which use a pattern to detect what to change (instead of a string).
For your string, it would render something like that:
<?php
$link = "http://example.com";
$site = file_get_contents($link);
// use a pattern
$ades = "/^<a href='http:\/\/example\.org\/'>.*<\/a>$/";
$bdes = "";
// use other function
$site = preg_replace($ades,$bdes,$site);
echo $site;
?>
Try this:
$var="blue";
$ades = "<a href='http://example.org/'>".$var."</a>";
$shortAdes=substr($ades,strpos($ades,$var),strlen($var));
echo $shortAdes;
it weill print the $var

PHP regex to exactly obtain a string I want

I have a code for embedding a link for iframe.
$post_contetn = explode('htt',$content);
$content_with_link = $post_contetn[0];
$link = 'htt'.$post_contetn[1];
But the problem is that, if I write
http://www.espn.com was great
then it links "was great" is part of the $link.
How can I change (perhaps use regex) to only include the actual url?
======
If I incorporate siam's answer, should it be
$regex = '/https?:\/\/.*?(?=\s)/';
$post_contetn = preg_match($regex, $content, $linkarray);
$content_with_link = $post_contetn[0];
$link = $linkarray[0]
echo $content_with_link;
I then edited to
preg_match($regex, $content, $post_contetn);
$content_with_link = $post_contetn[0];
$link = $post_contetn[0]
echo $content_with_link;
But the error still occurs at echo line.
Try using the following regex :
(?:https?:\/\/\S+)?\S+\.\S+\.?\S+
see demo / explanation
PHP
<?php
$content = 'http://www.espn.com was great';
$regex = '/(?:https?:\/\/\S+)?\S+\.\S+\.?\S+/';
preg_match($regex, $content, $post_contetn);
$link = $post_contetn[0];
echo $link;
?>

Extract the text from webpage

In this test.php page i have this line of text
server=span.growler.ro&provider=-1&providersSerial=4&country=RO&mobile=0&token=eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb
in this other test1.php?id=token page i have this php code runing
<?php
$Text=file_get_contents("./test.php");
if(isset($_GET["id"])){ $id = $_GET["id"];
$regex = "/".$id."=\'([^\']+)\'/";
preg_match_all($regex,$Text,$Match);
$fid=$Match[1][0];
echo $fid; } else { echo ""; } ?>
i need only the token
eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb
to be show on test1.php?id=token
if in test.php the token looks like this
token='eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb'
it works.
i needet to work from onother web page
$str = 'server=span.growler.ro&provider=-1&providersSerial=4&country=RO&mobile=0&token=eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb';
parse_str($str, $vars);
$token = $vars['token'];
using with preg_match will help you .
$string ='server=span.growler.ro&provider=-1&providersSerial=4&country=RO&mobile=0&token=eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb';
preg_match('/token=([a-f0-9]+)/i',$string,$matches);
echo $matches[1];
this will return you :
'eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb'
I'd recommend you to use preg_match instead of preg_match_all
Try this regex:
$regex = "/&?token=([a-f0-9]*)&?/;

how to print url from html code in php when url contain spaces

See i have an url in a html code
play
Now i want to print this url as it is written in a php page
http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3
You can see that between the url 05 Dabangg Reloaded their is space. I made this program to print url from this html code..
$str = "play";
$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;=?#.-]+)[^\w#$&+,\/:;=?#.-]*?`i';
if (preg_match_all($pattern,$str,$matches))
foreach($matches[1] as $data)
{
$str=$data;
echo $str;
}
Then i am getting this
http://b48.ve.vc/b/data/48/3746/05
please do not mention on foreach($matches[1] as $data) line bcoz i am using it with so many urls.. I just want to know how to print the whole url in this format.
http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3
Spaces are become a huge matter.. Do not know how to fix it..
What i need to add inside
$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;=?#.-]+)[^\w#$&+,\/:;=?#.-]*?`i';
For making it completely workable.
Please suggest me any idea.
$str = 'play';
$arr = explode("\"", $str);
$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;=?#.-]+)[^\w#$&+,\/:;=?#.-]*?`i';
$url = preg_grep($pattern,$arr);
$url = implode('',$url);
Output: $url = 'http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3'
Update: 2nd Solution [Reference-DOMElement].
$str = 'play';
$DOM = new DOMDocument;
$DOM->loadHTML($str);
$search_item = $DOM->getElementsByTagName('a');
foreach($search_item as $search_item) {
$url = $search_item->getAttribute('href');
}
echo $url; //Output: http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3
You can str_replace each one -space- with %20 for encoding your URL
<?php
$url_org = 'http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3';
$url_edited = str_replace(" ", '%20', $url_org);
?>
HERE
This will work.

Categories