Hi I'm developing a custom forum on my website. I would like to convert the urls starting with : http://*.domain.com/photos/{username}/{photo_id} (I should get开发者_JAVA技巧 both username and photo_id ) to the direct image tag so that user get the image instead of the url.
This should be done if they insert the url with or without bbcode:
ie: http://domain.com/photos/musthafa/12345 [url=http://domain.com/photos/musthafa/12345]my photo link here[/url] [url=http://domain.com/photos/musthafa/12345]http://domain.com/photos/musthafa/12345[/url]This should be converted to < html-imge tag src="url-to_photo-path/photo_id.j_p_g" />
I tried this:
$str = "http://www.domain.com/photos/musthafa/12345"
$str = preg_replace_callback("'\[url=http:\/\/www\.domain\.com\/photos\/(.*?)\](.*?)\[/url\]'i", 'self::parse_photo_url', $str);
AND
$str = preg_replace_callback("#^https?://([a-z0-9-]+\.)domain.com/photos/(.*?)$#", 'self::parse_gpp_photo', $str);
function parse_photo_url($url){
{
$full_url = "http://www.domain.com/" . $url[1];
$url_segs = parse_url($full_url);
$path = explode("/", $url_segs['path']);
return '<img src="http://www.domain.com/{path-to-the-gallery}/'.$path[2].'/jpg" />';
}
Musthafa.
I'm not sure that I've exactly got what you want but please try this:
<?php
$image_url_samples = array(
"http://domain.com/photos/musthafa/12345",
"[url=http://domain.com/photos/musthafa/12345]my photo link here[/url]",
"[url=http://domain.com/photos/musthafa/12345]http://domain.com/photos/musthafa/12345[/url]"
);
foreach ($image_url_samples as $image_url_sample)
{
$conversion_result = preg_replace("/^(http:\\/\\/domain.com\\/photos\\/\\w+\\/\\d+)$/i",
"<img src=\"\\1.jpg\" alt=\"\\1\" />", $image_url_sample);
$conversion_result = preg_replace("/^\\[url=(http:\\/\\/domain.com\\/photos\\/\\w+\\/\\d+)\\](.+)\\[\\/url\\]$/",
"<img src=\"\\1.jpg\" alt=\"\\2\" />", $conversion_result);
print $conversion_result . "<br />";
}
The output is:
<img src="http://domain.com/photos/musthafa/12345.jpg" alt="http://domain.com/photos/musthafa/12345" />
<br />
<img src="http://domain.com/photos/musthafa/12345.jpg" alt="my photo link here" />
<br />
<img src="http://domain.com/photos/musthafa/12345.jpg" alt="http://domain.com/photos/musthafa/12345" />
<br />
By the way, if you want to make your URL case insensitive add the i
modifier to the end of regular pattern (PHP Pattern Modifiers).
Basically I would like to extract the id (12345 in this example) and I need to extract the direct link to the image url so that, user should get the image tag instead of url.
Thats why I called
preg_replace_callback function.
In simple words, I'm stuck at the regex pattern to match my domain url starting with:
http://domain.com/photos{username}/{id}
OR
http://www.domain.com/photos{username}/{id}"
You don't need regex.
function buildLink( $id, $name ){
$html = array();
$html[] = '<img alt="" src="http://www.domain.com/photos/';
$html[] = $name;
$html[] = '/';
$html[] = $id;
$html[] = '.jpg">';
return implode( '', $html );
}
$path = 'http://www.domain.com/photos/musthafa/12345';
$path = rtrim( $path, ' /' );
$path_parts = explode( '/', $path );
$id = ( int ) array_pop( $path_parts );
$name = array_pop( $path_parts );
$img = buildLink( $id, $name );
echo $img;
精彩评论