There is a variable in which values ​​fall (addresses of sites). How to clear knowledge of a variable from unnecessary characters (http: //), if they were there, and also to display the message "the link contained http: //"

$link = "http://ru.stackoverflow.com/questions/ask" 

Closed due to the fact that the essence of the question is incomprehensible by the participants Alexey Shimansky , user194374, VenZell , pavel , lexxl 10 Aug '16 at 7:27 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Have you tried it yourself? - Naumov
  • I tried to search, I did not understand for half an hour. Since I do not yet know regular expressions. But there is little time left, it would be understandable that a day would have figured out - Evgeny Shevtsov
  • five
    @YevgeniyShevtsov, verification can be done without regular expressions, using strpos . Put a minus for the absolute unwillingness to understand the problem. - VenZell
  • Are you still boiling? Then we go to you: to work with url (validation, parsing) there are regular functions in the language. And you are regular, then str_replace ... And no one showed the topstarter with a hand in the direction filter_var, parse_url ... - AK
  • @AK I even wonder what kind of perversion you would write with parse_url to clear some extra characters from the value and also display a message about the content of these extra characters. about filter_var generally keep quiet. Do not restrain yourself - write the answer - Alexey Shimansky

3 answers 3

The optimal solution to the problem

 $cnt = 0; $link = str_replace("http://","",$link,$cnt); if($cnt) { echo "была замена"; } 

http://php.net/manual/ru/function.str-replace.php read docks. and you don't need castels with regulars which are a hundred times harder than just taking the number of replacements from str_replace

p.c. I’ll use the answer to clear the url

 $urlNew = filter_var($url,FILTER_SANITIZE_URL); if(strlen($urlNew) != strlen($url)) { echo "Была замена"; } 
  • one
    I think the vehicle only in the question specifically indicated http:// . It seems to me that in the end it will be exactly the set of different characters that will be searched for and output exactly that the "xy 1123 characters were in the line, sorry sorry" ... and then you will definitely need regulars for this. But it seems to me) - Alexey Shimansky

Profit (seems to be working):

 $link = str_replace("http://","",$link); 

    Well, like this:

     if (preg_match('/^http\:\/\//', $link, $matches)) { $link = str_replace($matches[1], '', $link); echo 'Ссылка содержала http://'; } 

    (a question the answer :)

    • what you need))) - Evgeny Shevtsov
    • Compare regular and then do. Ps the condition is generally superfluous - Naumov
    • @Naumov condition is not superfluous. Topstarter wanted to display a message if there was a replacement. - toxxxa
    • That is still a minus to check if you should use strpos but not like a regular program. - Naumov
    • one
      in a particular case, yes, but the question was too general “to find the characters”. I realized that "for example http: // but maybe something else." - toxxxa