How do I get a URL in a string of input using PHP

Posted on 2011-04-27
Last Modified: 2012-05-11
I have string like this,  "This is the string texts from:  " .. how do i get only the URL from this string as "" ...
Question by:Loganathan Natarajan
    LVL 2

    Expert Comment

    use string operations to search or do regular expression matching.
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    you have a couple of methods, starting with parse_url:

    LVL 2

    Expert Comment

    LVL 30

    Accepted Solution

    This works fine:

    $text =  "This is the string texts from:  ";
    preg_match('/\b((?#protocol)https?|ftp):\/\/((?#domain)[-A-Z0-9.]+)((?#file)\/[-A-Z0-9+&@#\/%=~_|!:,.;]*)?((?#parameters)\?[A-Z0-9+&@#\/%=~_|!:,.;]*)?/i', $text, $match);
    $url = $match[0];
    echo $url;

    LVL 12

    Expert Comment

    by:Mohamed Abowarda
    I have answered this question here:
    $string = "This is the string texts from:";
    $strArray = explode(' ', $string);
    foreach($strArray as $word)
    	if (substr($word, 0, 7) == "http://" || substr($word, 0, 8) == "https://")
    		$url = $word;
    echo "Your URL: $url<br />";

    Open in new window

    LVL 36

    Author Closing Comment

    by:Loganathan Natarajan
    thanks, this works perfect for dynamically change text and url...
    LVL 36

    Author Comment

    by:Loganathan Natarajan
    @ Medo3337

    tried already like that , the prb is we can't fix the location or separator .. it may vary .. i was looking preg_match it

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now