How to use substr at regexp of preg replace - php

i am making a bbcode for youtube videos.User can post a video as bbcode eg like [youtube]http://www.youtube.com/watch?v=ihK2pPcDSHM[/youtube]. Next, it will convert it to html code.But instead of video,i want to show also the image of the video. So i do it like this:
$string = preg_replace("~\[yt]http://www.youtube.com/watch\?v=(.*)\[/yt]~Uis","<img src=\"http://img.youtube.com/vi/\\1/0.jpg\" />", $string);
It shows the image, but when somebody puts a url like:
http://www.youtube.com/watch?v=ihK2pPcDSHM&feature=channel
Then the image url becomes http://img.youtube.com/vi/ihK2pPcDSHM&feature=channel1/0.jpg which does not lead to a valid image. I am trying to change the \\1 to ".substr('\\1', 0,11)." but it doesnt have any result.
Any suggestion to solve this? Thanks!

Try a different pattern like:
~\[yt]http://www\.youtube\.com/watch\?v=([a-z0-9-_]+).*?\[/yt]~is

Just tell your regex to stop on the first & character:
$string = preg_replace("~\[yt]http://www.youtube.com/watch\?v=([^\\&]*)\[/yt]~Uis","<img src=\"http://img.youtube.com/vi/\\1/0.jpg\" />", $string);

Related

Regex / BBCode Parser to find string between two strings (Between two strings) and replace it

I couldn't come up with a better title, my apologies. Basically, we're going to have text that looks like this:
Wow, thanks for that image. It really helps!
[quote]Here, this image may help you.
[img]http://www.url.to.image.jpg[/img]
[/quote]
The text could also appear as
Wow, thanks for that image. It really helps!
[quote="username"]Here, this image may help you.
[img]http://www.url.to.image.jpg[/img]
[/quote]
So, what we're wanting to do is grab any images that are inside a quote and replace those [img] tags with [url=http://www.url.to.image.jpg]Click here to view the image[/url]. But this operation should ONLY happen for images inside quote tags. I've looked at the various BBCode parsers for PHP but can't find anything that would be able to do this, and I'm unsure of the regex required for such a task.
You could try:
(\[quote[^]]*].*?)(?=\[img])\[img](.*?)\[/img]
with replacement string
$1[url=$2]Click here to view the image[/url]
Not sure of the code. Perhaps:
$result = preg_replace('%(\[quote[^]]*].*?)(?=\[img])\[img](.*?)\[/img]%sim', '$1[url=$2]Click here to view the image[/url]', $subject);
We must ensure that there is no [/quote] before [img]. This can be done by not using .*?, but ((?!\[/quote]).)*
$regex = '#(\[quote[^]]*]((?!\[/quote]).)*)\[img](.*?)\[/img]#s';
$replace = '$1[url=$3]Click here to view the image[/url]';
$str = preg_replace($regex, $replace, $str);

preg_match and images URL

I have a little problem with preg_match function in PHP. I think that I never will learn how to use this function. I want to extract URL of image from HTML without name of image. For example, if I have some link for image:
"/data/images/2013-10-03/someimage.jpg"
or
"http://something.com//data/images/2013-10-03/someimage.jpg"
How can I use preg_match function to delete everything left of last forward slash, so I can get only image name from URL?
Maybe it's smarter to use different function but I dont know which one?
P.S. Can you give me some good tutorial for preg_match function?
Maybe I forgot to say... I dont know how long is image name or what is image name exactly. I need function for extract only what is on right side from last forward slash.
$pattern = '/[\w\-]+\.(jpg|png|gif|jpeg)/';
$subject = 'http://something.com//data/images/2013-10-03/someimage.png';
$result = preg_match($pattern, $subject, $matches);
echo $matches[0]; //someimage.jpg
No need for regex or anything fancy:
$var = "http://something.com/data/images/2013-10-03/someimage.jpg";
$image = basename($var);
U need use preg_replace() and u can try use online for play with regular, it is a fast way to learn regex. http://preg_replace.onlinephpfunctions.com/
For example: /\/someimage.jpg/ replace on ''(null).
It will return http://something.com//data/images/2013-10-03 from http://something.com//data/images/2013-10-03/someimage.jpg.
You can use Simple HTML DOM Parser to get href between the a tags.
For example:
foreach($html->find('a.[class="your class"]') as $var)
// echo "href." >sometext";
hope this helps!

Using preg_match and stopping at image SRC

So I am using the preg_match() function to extract the SRC file name of an image and that part is working well.
The problem though is it isn't stopping at the closing quotation marks of the src field. It keeps going to including "some" other stuff in the image tag but then just stops. I really just want the src URL only.
preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $rss, $image);
echo $image['src'];
Try out this, this may work
$regex = '/src="(.+?)"/';
then preg_match($regex,$rss,$image);
i am doing this with;
preg_match('#src.=.".*"',$stringToMatch,$result);
Besides I don't advise to parse HTML like this, but this would work for you;
$str = '<img src="to/file/img.png" width="100">';
preg_match('~<img.*?src=[\'"]*(?P<src>[^\s\'"]*)~i', $str, $match);
print_r($match);

Formatting text with regex

I have two functions to format the text of my notices.
1. Converts [white-text][/white-text] into <font color=white></font>
$string = preg_replace("/\[white-text\](\S+?)\[\/white-text\]/si","<font color=white>\\1</font>", $string);
2. Converts [url][/url] into <a href></a>
$string = preg_replace("/\[url\](\S+?)\[\/url\]/si","\\1", $string);
Problems:
WHITE-TEXT - It only changes the color if the phrase has only ONE word.
URL - It works fine, but I would like to be able to write anything in the readable part of the URL.
URL - It works fine, but I would like to be able to write anything in the readable part of the URL.
Make the URL code have the form [url=href]description[/url], you can then use this simple RegExp
"/\[url=([^\]]*)\](.+?)\[\/url\]/si"
"\\2"

Validate URL and display as a link in PHP

When I display the text from database, I want to detect whether that text is URL and if that's with URL format, i want to hyperlink those text automatically.
For example, if my text is like this
"Hey, check this out, i found a great website and i would like to share with you all. Here is the website www.google.com"
So in the above text, I would like to hyperlink www.google.com to www.google.com
Which method should i use to detect url format and adding hyperlink ?
Please kindly suggest. Thank you.
function makeClickableLinks($text) {
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9#:%_\+.~#?&//=]+)', '\\1', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9#:%_\+.~#?&//=]+)', '\\1\\2', $text);
$text = eregi_replace('([_\.0-9a-z-]+#([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '\\1', $text);
return $text;
}
This is the right one ;-) works for all HTTP links (with or without http://) and for e-mail links. Usage echo makeClickableLinks($string);
It does not support https as I see, the code is from http://www.totallyphp.co.uk/code/convert_links_into_clickable_hyperlinks.htm here and seems to work. At least this kicks you in the right direction.
you could use this code snippet:
$text = preg_replace('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', '$1', $text);
found on snipplr.com
This can be done with regular expressions. Something along the lines of:
echo preg_replace("%((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)%ie", "$3$4",$text);
*Edit: Updated regex

Categories