2010-06-07 5 views
2

J'ai une chaîne comme ce The theme song of whatever - http://www.anydomain.com/pop_new.php?sid=10623&aid=1581&rand=0.6808111508818073 #stringPHP get url sur une chaîne et un peu plus

Et maintenant, je dois faire la chose suivante.

  1. Obtenir l'URL de dessus chaîne http://www.anydomain.com/pop_new.php?sid=10623&aid=1581&rand=0.6808111508818073
  2. Remplacez l'URL {% url%} il devrait donc ressembler à The theme song of whatever - {%url%} #string

Actuellement, je suis en utilisant le code suivant, mais il ne parvient pas à remplacer la ci-dessus url.

$urlregex_ = "(https?)\:\/\/[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?"; 
preg_match('~'.$urlregex_.'~',preg_replace('/\+/',' ',$url),$url_only); 
$url_ = preg_replace('/ /','+',$url_only[0]); 
$text = preg_replace('~'.$url_.'~','{%url%} ',$url); 
return array('url' => $url_only[0], 'text' => $text);` 

Espoir vous pouvez aider, merci, pnm123

Répondre

2
<?php 
    $orig = "The theme song of whatever - http://www.anydomain.com/pop_new.php" . 
     "?sid=10623&aid=1581&rand=0.6808111508818073 #string"; 
    $urlregex = '~(?:https?)://[a-z0-9+$_-]+(?:\\.[a-z0-9+$_-]+)*' . 
     '(?:/(?:[a-z0-9+$_-]\\.?)+)*/?(?:\\?[a-z+&$_.-][a-z0-9;:@/&%=+$_.-]*)?'. 
     '(?:#[a-z_.-][a-z0-9+$_.-]*)?~i'; 
    if (preg_match($urlregex, $orig, $matches)) { 
     $after = preg_replace($urlregex, "{%url%}", $orig); 
     var_dump(array('url' => $matches[0], 'text' => $after)); 
    } else { //no array found 
     die("oops"); 
    } 
+0

voudrez peut-être mentionner que 'mourir()' est rarement une bonne façon de gérer quelque chose d'inattendu. – alex

+0

@alex, c'est juste un exemple. Je ne m'attends pas à ce qu'il utilise 'var_dump' non plus. C'est ce que j'ai fait pour tester. – Artefacto

+0

Cela vient de résoudre mon problème pour l'instant ... Merci ... – pnm123