Tag Archive for extract

One liner perl extract content with regular expression

curl -s <a href="http://checkip.dyndns.org" >http://checkip.dyndns.org</a> | perl -nle 'print "$1" if (/Current IP Address: ([d.]*)/)'

source

Extract urls between anchor tags using php

/**
Returns an array containing each of the sub-strings from text that
are between openingMarker and closingMarker. The text from
openingMarker and closingMarker are not included in the result.
This function does not support nesting of markers.
*/
function returnSubstrings($text, $openingMarker, $closingMarker) {
$openingMarkerLength = strlen($openingMarker);
$closingMarkerLength = strlen($closingMarker);

$result = array();
$position = 0;
while (($position = strpos($text, $openingMarker, $position)) !== false) {
$position += $openingMarkerLength;
if (($closingMarkerPosition = strpos($text, $closingMarker, $position)) !== false) {
$result[] = substr($text, $position, $closingMarkerPosition - $position);
$position = $closingMarkerPosition + $closingMarkerLength;
}
}
return $result;
}
$msg = "This is a string with a url in <a href="http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N">http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N</a>"

$urls = returnSubstrings($msg,'">','</a>');

print_r($urls);

// array (
//   0 => 'http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N'
// )
//

source

Query Variable Function

variable = getQueryVariable("what you want to extract");//e.g "source"

function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
}

source