How to check if it's a link or not in text ?

Discussion in 'PHP' started by genetrix, Oct 24, 2008.

  1. #1
    Hi , how to check if it's a link or not in the text ?
    I used Preg_match function , but I think there are other nice and smart methods in PHP

    Any help appreciated , thanks.
     
    genetrix, Oct 24, 2008 IP
  2. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if what's a link or not in what text?
     
    Kyosys, Oct 24, 2008 IP
  3. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    preg_match is what I would use as well.
     
    keyaa, Oct 24, 2008 IP
  4. genetrix

    genetrix Banned

    Messages:
    400
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I mean I'm searching for other ways , if you have some examples (not preg_match) please post them .

    Thanks.
     
    genetrix, Oct 24, 2008 IP
  5. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'd help you, but I have no idea what you are asking
     
    Kyosys, Oct 24, 2008 IP
  6. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Why do you think preg_match is not smart to begin with? It does exactly what you want.
     
    keyaa, Oct 24, 2008 IP
  7. Equaite

    Equaite Active Member

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    Digital Goods:
    1
    #7
    preg_match is highly efficient and a good solution, as it does not cause the script to execute slowly or lag. If you are looking to check whether a variable itself is a valid URL, as opposed to a body of text containing URLs, then you could try this:

    <?php
    	#checks a variable and determines if it is valid or not
    	function valid_url ($url) {
    		return count(@parse_url($valid_url)) > 1;
    	}
    	$valid_url = 'http://www.example.com/';
    	$invalid_url = 'example.com/help/topics/138/';
    	if (valid_url($valid_url)) {
    		echo "$valid_url is a valid URL.<br />\n";
    	}
    	if (valid_url($invalid_url)) {
    		echo "$invalid_url is not a valid URL.";
    	}
    ?>
    PHP:
     
    Equaite, Oct 24, 2008 IP
  8. genetrix

    genetrix Banned

    Messages:
    400
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thank you for example !
     
    genetrix, Oct 27, 2008 IP
  9. Equaite

    Equaite Active Member

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    Digital Goods:
    1
    #9
    No problem, always happy to help. :)
     
    Equaite, Oct 27, 2008 IP