i have a long string which contains a url. I want to extract that url from that particular string. Please help me.

Dani AI

Generated

Short answer: treat the input as HTML and use a DOM parser instead of exploding by spaces or doing ad-hoc substrings. , and pointed to tokenizing/regex/explode approaches that can work on very simple text, but they break on real HTML (attributes, spacing, entity encoding). 's parse_url idea is meant for raw URLs, not HTML fragments, so it won't reliably extract an anchor element.

A robust approach uses PHP's DOMDocument + DOMXPath to find anchors whose href starts with mailto (case-insensitive) and then validate the email with filter_var:

<?php
$html = '<your HTML here>';

libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML('<?xml encoding="utf-8" ?>' . $html);
$xpath = new DOMXPath($doc);

// find <a> where href begins with "mailto:" (case-insensitive)
$nodes = $xpath->query("//a[starts-with(translate(@href,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'mailto:')]");

$results = [];
foreach ($nodes as $a) {
    $href = $a->getAttribute('href');
    $email = preg_replace('/^mailto:/i', '', html_entity_decode($href, ENT_QUOTES, 'UTF-8'));
    $text  = trim($a->textContent);
    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $results[] = ['email' => $email, 'text' => $text, 'href' => $href];
    }
}

print_r($results);

If DOMDocument is unavailable or the input is tiny and guaranteed well-formed, a single preg_match fallback can capture the first mailto anchor, but regex on HTML is fragile and should be a last resort.

Troubleshooting notes: suppress libxml warnings for fragments, decode HTML entities before validation, handle multiple anchors, remember display text and href can differ, and use an HTML tidy step if input is badly malformed. For plain tokenized text the explode/preg idea from can be faster, but for any real HTML the DOM approach above is the safer, maintainable choice.

Recommended Answers

All 8 Replies

i have a long string which contains a url. I want to extract that url from that particular string. Please help me.

Hi ditty....

U can extract by using this way....

1) by using EXPLODE function
2)by using SPLIT function...

eg:

$url="http://info@abc.com";
list(first,second)=split("@",$url,2)
echo $second;

or

$a=explode('@',$url);
$a[1];

sorry....i mean i have a string for example

ssfihuihuiifhhdfhishfihfh jjjjjif jfiosofjojofjjm ojofjojfc ojjofjojw

From this i want to extract

If the string is in this format

dddddddeeeeeeeffff

I don't think you can extract the email address, since you wouldn't know what is the exact part before @. If your string is as you stated above, ie.,

ssfihuihuiifhhdfhishfihfh jjjjjif jfiosofjojofjjm ojofjojfc ojjofjojw

then you can explode the string to an array, then check each array element (with regular expression), if it contains a valid email address.

sorry....i mean i have a string for example

ssfihuihuiifhhdfhishfihfh jjjjjif jfiosofjojofjjm ojofjojfc ojjofjojw

From this i want to extract

Hi..

can u try to using SUBSTRING FUNCTIONS ..i dont know the exact solution..but, if string having hdgfhgfgjh hdgfhdgfhdfg ....than u can do...ok...

any body knows good solution,plz solve this one...

Hi,

First explode the string " hdgfhgfgjh hdgfhdgfhdfg " by using " " (space).....

There at one extent you will find the URL (i.e) Explode will return an array.....

By checking an array element with preg_match() to find the URL....

Once you found the URL, break the loop


Regards,
Fatty

<?php

    $originalString = "ssfihuihuiifhhdfhishfihfh jjjjjif [email]ee@gg.com[/email] jfiosofjojofjjm ojofjojfc ojjofjojw ";

    $stringToArray= explode(" ",$originalString);

    echo "<pre>".print_r($stringToArray,1)."</pre>";

    foreach($stringToArray as $key=>$val){
        $URL_Validation=ereg("^[^@ ]+@[^@ ]+\.[^@ ]+$",$val,$trashed);
        if($URL_Validation){
            $URL = $val;
            break;
        }
    }

    echo "URL : ".$URL;

?>

Hope this code will help u :)

Regards,
Fatty

phpinfo();

hello... you have several options to implement this. use preg_replace function or use parse_url function. below i have explained it further.

$tempText = "hello.. how are you?? are you keeping well?? please checkout this url http://www.youtube.com/watch?v=ehuwoGVLyhg&feature=topvideos";

print_r(parse_url($tempText,PHP_URL_PATH));

hope this would be useful.
Thanks!
Asoka

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.