Jump to content
php.lv forumi

linku konvertēšana


ziedinjsh

Recommended Posts

Sveiki!

funkcija

function media_check($name){
if($name = preg_replace('/((https?|ftp).*(gif|png|jpg|jpeg))/i', '<img src="$1" />', $name)){
$result = $name;
}elseif($name = preg_replace('/([\w\.-]+)(@)([\w\.-]+)/e', '"<a href=\"mailto:\\0\">\\0</a>"', $name)){
$result = $name;
}elseif($name = preg_replace('/( http:\/\/)([\w\.-]+)/e', '"<a href=\"\\0\" target=\"_blank\">\\0</a>"', $name)){
$result = $name;
}elseif($name = preg_replace('/( www.)([\w\.-]+)/e', '"<a href=\"http://\\0\" target=\"_blank\">\\0</a>"', $name)){
$result = $name;
}
return $result;

}

 

 

Kapēc viņš linkus nepārkonvertē par hiperlinku?

Link to comment
Share on other sites

un kā tad es varu viņus izvadīt kā vienu?

:) Nepārbaudīju vai baigi pareizi darbojas $matches un tā, bet apmēram tā. array() vispirms ņem pirmo, tad otro un tāda garā.

<?php

function media_check($status_text)
{
   	$matches = array(
'/((https?|ftp).*(gif|png|jpg|jpeg))/i',
'/([\w\.-]+)(@)([\w\.-]+)/e',
'/( http:\/\/)([\w\.-]+)/e',
'/( www.)([\w\.-]+)/e',
   	);

   	$replacements = array(
'<img src="$1" />',
'"<a href=\"mailto:\\0\">\\0</a>"',
'"<a href=\"\\0\" target=\"_blank\">\\0</a>"',
'"<a href=\"http://\\0\" target=\"_blank\">\\0</a>"',
   	);

$status_text = preg_replace($matches, $replacements, $status_text);

 return $status_text;
}

$rinda='[email protected] http://bh.id.lv/epasts.png  http://bh.id.lv ';

echo media_check($rinda);


?>

Edited by blackhalt
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...