i need to use regular expression to find links with php my html code: 10px;"> <div class="post-title" style="margin: -5px 1px 2px 0px;"><h2><a href="https://www.tradebit.com/filedetail.php/276221757-local-marketing-goldrush"> Local Marketing Goldrush</a></h2></div> <div style="line-height: 1.4em; font-size: 1.2em;"> Code (markup): i need to receive this link : https://www.tradebit.com/filedetail.php/276221757-local-marketing-goldrush
You could find links with <?php $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>"; PHP: However I would recommend using xpath for parsing as it is much easier http://php.net/manual/en/class.domxpath.php
As an alternative to regex and dom there's also php's string position functions you could consider to. Try this for example; <?php $subject = '10px;"> <div class="post-title" style="margin: -5px 1px 2px 0px;"><h2><a href="https://www.tradebit.com/filedetail.php/276221757-local-marketing-goldrush"> Local Marketing Goldrush</a></h2></div> <div style="line-height: 1.4em; font-size: 1.2em;">'; $start = strpos($subject,'a href="'); $end = strpos($subject,'">',$start); $output = substr(substr($subject, $start, $end - $start), 8); echo $output; ?> PHP: You can apply simple math to extract the url this way, for multiple returns though you'd need to bundle code into a function and call via an array. .
Another alternative is to use DOMDocument which has a javascript feel about it. Where I was searching for 'img' you'd search for 'a' $document = new DOMDocument(); libxml_use_internal_errors(true); $document->loadHTML($html_source); libxml_clear_errors(); $images = array(); foreach($document->getElementsByTagName('img') as $img) { $src = $img->getAttribute('src'); if (!empty($src)) { $images[$src] = "<meta property='og:image' content='{$src}' />"; } } PHP: