$str = 'window.location.href="http://my-site.com";'
I want to extract the url from $str. I am not that good in preg_match(). However with the following code:
preg_match('/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/', $str, $link);
if (empty($link[0])) {
echo "Nothing found!";
} else {
echo $link[0];
}
I am able to get the result http://my-site.com";. I want to customize preg_match() to exclude "; from the result. Please help!
<?php
$str = 'window.location.href="http://my-site.com";';
preg_match('/window\.location\.href="(.*?)";/', $str, $result);
echo $result[1];
//http://my-site.com
>?
http://ideone.com/YTk70i
If you dont feel comfortable with preg_* then try keeping it simple. It seems a bit of an unnecessary overhead loading the regex engine anyway for something that simple.
Try this instead :-
$str = 'window.location.href="http://my-site.com";';
$p1 = strpos($str, 'href="') + strlen('href="');
$p2 = strpos($str, '";', $p1);
$url = substr($str,$p1,$p2-$p1);
echo $p1 .PHP_EOL;
echo $p2 .PHP_EOL;
echo $url;
This yeilds the following
22
40
http://my-site.com
i.e everything between href=" and ";
Try this:
preg_match('/^window.location.href="([^"]+)";$/', $str, $link);
Related
need to extract an info from a string which strats at 'type-' and ends at '-id'
IDlocationTagID-type-area-id-492
here is the string, so I need to extract values : area and 492 from the string :
After 'type-' and before '-id' and after 'id-'
You can use the preg_match:
For example:
preg_match("/type-(.\w+)-id-(.\d+)/", $input_line, $output_array);
To check, you may need the service:
http://www.phpliveregex.com/
P.S. If the function preg_match will be too heavy, there is an alternative solution:
$str = 'IDlocationTagID-type-area-id-492';
$itr = new ArrayIterator(explode('-', $str));
foreach($itr as $key => $value) {
if($value === 'type') {
$itr->next();
var_dump($itr->current());
}
if($value === 'id') {
$itr->next();
var_dump($itr->current());
}
}
This is what you want using two explode.
$str = 'IDlocationTagID-type-area-id-492';
echo explode("-id", explode("type-", $str)[1])[0]; //area
echo trim(explode("-id", explode("type-", $str)[1])[1], '-'); //492
Little Simple ways.
echo explode("type-", explode("-id-", $str)[0])[1]; // area
echo explode("-id-", $str)[1]; // 492
Using Regular Expression:
preg_match("/type-(.*)-id-(.*)/", $str, $output_array);
print_r($output_array);
echo $area = $output_array[1]; // area
echo $fnt = $output_array[2]; // 492
You can use explode to get the values:
$a = "IDlocationTagID-type-area-id-492";
$data = explode("-",$a);
echo "Area ".$data[2]." Id ".$data[4];
$matches = null;
$returnValue = preg_match('/type-(.*?)-id/', $yourString, $matches);
echo($matches[1]);
I Have a problem with this output receive value.
$simple="<TRAN_ID>17564_36428.1354_4159</TRAN_ID>
<TRAN_DATE>20160201</TRAN_DATE>
<TRAN_TIME>10:07:08</TRAN_TIME>
<ERROR_CODE>1</ERROR_CODE>
<ERROR_DESC>Not Input Policy</ERROR_DESC>
<POLICY_NBR></POLICY_NBR>";
I want to cut the code with PHP.
TRAN_ID = ?
TRAND_DATE = ?
ERROR_CODE = ?
ERROR_DESC = ?
How can i do it. sorry my english is bad.
Thanks.
You can use PHP's SimpleXML library, like so:
<?php
$str ="<TRANS><TRAN_ID>17564_36428.1354_4159</TRAN_ID><TRAN_DATE>20160201</TRAN_DATE><TRAN_TIME>10:07:08</TRAN_TIME><ERROR_CODE>1</ERROR_CODE><ERROR_DESC>Not Input Policy</ERROR_DESC><POLICY_NBR></POLICY_NBR></TRANS>";
$transaction = simplexml_load_string($str);
echo $transaction->TRAN_ID.PHP_EOL;
echo $transaction->TRAN_DATE.PHP_EOL;
echo $transaction->TRAN_TIME.PHP_EOL;
echo $transaction->ERROR_CODE.PHP_EOL;
echo $transaction->ERROR_DESC.PHP_EOL;
echo $transaction->POLICY_NBR.PHP_EOL;
Note that I added <TRANS> start and end tags to your string.
If the data always looks like the sample, this should work okay.
<?php
function getTextBetweenTags($string, $tagname) {
$pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
$str = '<textformat leading="2"><p align="left"><font size="10">get me</font></p></textformat>';
$txt = getTextBetweenTags($str, "font");
echo $txt;
?>
I am trying to find a way to replace a string as the following:
Original string:
|Hello||everybody|, I am |human|
And result:
<span>Hello</span><span>everybody</span>, I am <span>human</span>
Is there a simple way to replace this original string to this result.
Thanks in advance.
preg_replace(
"~\|(.+)\|~U",
"<span>$1</span>",
$yourString
);
ideone demo.
I'm not really good at regexp, so here is an other code:
$string = "|Hello||everybody|, I am |human|";
$arr = explode("|", $string);
$result = "";
$span = "<span>";
$span_close = "</span>";
foreach($arr as $element){
if(strlen($element) > 0){
$result .= $span.$element.$span_close;
}
}
echo $result;
How can I extract 4 from this string?
$string = "Rank_1:1:4";
I'm trying to get pagerank from Googles server, and the last value (4) is the actual pagerank.
Try
$string = "Rank_1:1:4";
$data = explode(':',$string);
echo end($data);
EDIT
as per #MichaelHampton, if they add more fields later, then use as below
$string = "Rank_1:1:4";
$data = explode(':',$string);
echo $data[2];
PHP has so many string function you can use ...
Variables
$find = ":";
$string = "Rank_1:1:4";
Using substr
echo substr($string, strrpos($string, $find) + 1);
Using strrchr
echo ltrim(strrchr($string, $find),$find);
$pattern = '/:\d+$/';
preg_match($pattern, $string, $matches);
$rank = substr($matches[0],1);
I have the following, simple code:
$text = str_replace($f,''.$u.'',$text);
where $f is a URL, like http://google.ca, and $u is the name of the URL (my function names it 'Google').
My problem is, is if I give my function a string like
http://google.ca http://google.ca
it returns
Google" target="_blank">Google</a> Google" target="_blank">Google</a>
Which obviously isn't what I want. I want my function to echo out two separate, clickable links. But str_replace is replacing the first occurrence (it's in a loop to loop through all the found URLs), and that first occurrence has already been replaced.
How can I tell str_replace to ignore that specific one, and move onto the next? The string given is user input, so I can't just give it a static offset or anything with substr, which I have tried.
Thank you!
One way, though it's a bit of a kludge: you can use a temporary marker that (hopefully) won't appear in the string:
$text = str_replace ($f, '' . $u . '',
$text);
That way, the first substitution won't be found again. Then at the end (after you've processed the entire line), simply change the markers back:
$text = str_replace ('XYZZYPLUGH', $f, $text);
Why not pass your function an array of URLs, instead?
function makeLinks(array $urls) {
$links = array();
foreach ($urls as $url) {
list($desc, $href) = $url;
// If $href is based on user input, watch out for "javascript: foo;" and other XSS attacks here.
$links[] = '<a href="' . htmlentities($href) . '" target="_blank">'
. htmlentities($desc)
. '</a>';
}
return $links; // or implode('', $links) if you want a string instead
}
$urls = array(
array('Google', 'http://google.ca'),
array('Google', 'http://google.ca')
);
var_dump(makeLinks($urls));
If i understand your problem correctly, you can just use the function sprintf. I think something like this should work:
function urlize($name, $url)
{
// Make sure the url is formatted ok
if (!filter_var($url, FILTER_VALIDATE_URL))
return '';
$name = htmlspecialchars($name, ENT_QUOTES);
$url = htmlspecialchars($url, ENT_QUOTES);
return sprintf('%s', $url, $name);
}
echo urlize('my name', 'http://www.domain.com');
// my name
I havent test it though.
I suggest you to use preg_replace instead of str_replace here like this code:
$f = 'http://google.ca';
$u = 'Google';
$text='http://google.ca http://google.ca';
$regex = '~(?<!<a href=")' . preg_quote($f) . '~'; // negative lookbehind
$text = preg_replace($regex, ''.$u.'', $text);
echo $text . "\n";
$text = preg_replace($regex, ''.$u.'', $text);
echo $text . "\n";
OUTPUT:
Google Google
Google Google