I have few functions that sanitize everything in get and post but this one must be bit more specific , I have an array that is producing links like
array(
1 => http://site/something/another/?get_something=1
2 => http://site/something/another2/?get_something=1
)
also have a function that is sanitizing get and post but that function would clear everything from this array values so I am left with
httpsitesomethinganotherget_something1
can someone please help to match EVERYTHING after get_something=
and clear it or replace it with get_something=1 , something like
$clear = preg_match('/^[A-Za-z0-9]+$/', $array[1]);
preg_replace("/get_something=".$clear."/i","",$array[1]);
You should use parse_url but here is a regex implementation, in case you're curious:
$urls = array
(
'http://site/something/another/?get_something=1',
'http://site/something/another2/?get_something=1',
'http://site/something/another/?get_something=1&foo=bar',
'http://site/something/another/?foo=bar&get_something=1',
);
$urls = preg_replace('~[?&]get_something=.*$~i', '', $urls);
print_r($urls);
Should output (demo [updated]):
Array
(
[0] => http://site/something/another/
[1] => http://site/something/another2/
[2] => http://site/something/another/
[3] => http://site/something/another/?foo=bar
)
If you're trying to parse the URL, your best bet is to use parse_url, lop off the query elements you don't want, and rebuild with http_build_url. Here's how I'd do it:
for($i = 0; $i < count($linkArray); $i++) {
// get original link and parse it
$link = parse_url($linkArray[$i]);
// replace the query portion
$link["query"] = "get_something=1";
// build the new URL
$linkArray[$i] = $link["scheme"] . "://" . $link["hostname"] . $link["path"] . "?" . $link["query"] . "#" . $link["fragment"]
// done
}
Related
I have a pretty large array that I would need to parse, but the request changes depending on enabled/disabled parameters.
For example:
$array['a'][1]['b'][1]['c'][1] = 'asd';
$str = $array['a'][1];
dd($str);
This would give me:
Array
(
[b] => Array
(
[1] => Array
(
[c] => Array
(
[1] => asd
)
)
)
)
Which, of course, is correct. But now, if I know, that I need also the next parameter, I would need to add that like $str = $array['a'][1]['b'];.
But since there are way too many combinations, I wondered, if I can construct the call manually, something like this":
$str = $array['a'][1];
if ($b) {
$str .= ['b'][1];
}
if ($c) {
$str .= ['c'][1];
}
dd($str);
Any hints will be appreciated.
PS: I know I can do this with eval, but was really looking for a better solution:
eval("\$str = \$array$str;");
dd($str);
It can be done with Reference
$string = "['a'][1]['b'][1]";
// Maybe, not optimal, but it's not the point of the code
preg_match_all('/\[\'?([^\]\']+)\'?\]/', $string, $steps);
// "Root" of the array
$p = &$array;
foreach($steps[1] as $s) {
// Next step with current key
if (isset($p[$s]) && is_array($p)) $p = &$p[$s];
else throw new Exception("No such item");
}
// Target value
$str = $p;
demo
I have an array in PHP $aResults full thousands of URLs that looks like this:
Array ( [0] => http://test.com/server1/Image?img=nortel.jpg )
Array ( [1] => http://test.com/server1/Image?img=network.jpg )
I need to replace the text inside each url and replace the server from server1 to server5 and the word image with photo so each url should look like this:
http://test.com/server5/photo?img=
How do i accomplish this?
I have tried a variation of str_replace functions but i cannot get this work:
$sImgURL = $aResults[1][0];
$filter_url ='server1/Image';
$replace='server5/photo';
$filtered_url = str_replace($filter_url, $replace, $aResults);
print_r($aResults);
What is the best way to accomplish this? Thanks
for ($i = 0; $i <= sizeof($aResults); $i++) {
$aResults[$i] = str_replace('server1/Image', 'server5/photo', $aResults[$i]);
}
You could use array_map and str_replace functions:
<?php
$data = array(
0 => 'http://test.com/server1/Image?img=nortel.jpg' ,
1 => 'http://test.com/server1/Image?img=network.jpg',
2 => 'http://test.com/server1/Image?img=rk.jpg',
3 => 'http://test.com/server1/Image?img=nek.jpg'
);
function foo($val){
return str_replace(
array('server1','Image'),
array('server5','photo'),
$val
);
}
$res = array_map("foo",$data);
var_dump($res);
?>
As Rizier123 suggest, you can do it in one line by:
var_dump(str_replace('server1/Image','server5/photo',$data));
Put your values to 'find' and your values to 'replace' into arrays..
$find = array('server1', 'Image');
$replace = array('server5', 'photo');
$newString = str_replace($find, $replace, $oldString);
I've been searching and searching and can't find anything that works, but this is what I want to do.
This code:
try{
$timeout = 2;
$scraper = new udptscraper($timeout);
$ret = $scraper->scrape('udp://tracker.openbittorrent.com:80',array('0D7EA7F06E07F56780D733F18F46DDBB826DCB65'));
print_r($ret);
}catch(ScraperException $e){
echo('Error: ' . $e->getMessage() . "<br />\n");
echo('Connection error: ' . ($e->isConnectionError() ? 'yes' : 'no') . "<br />\n");
}
Outputs this:
Array ( [0D7EA7F06E07F56780D733F18F46DDBB826DCB65] => Array ( [seeders] => 148 [completed] => 10 [leechers] => 20 [infohash] => 0D7EA7F06E07F56780D733F18F46DDBB826DCB65 ) )
And I want that seeder count into a string such as $seeds. How would I go about doing this?
Something like this?
$seeds = $ret['0D7EA7F06E07F56780D733F18F46DDBB826DCB65']['seeders'];
you can user strval() to convert a number to a string.
$string = strval($number);
or you can cast it into a string:
$string = (string)$number;
in your context that would be:
$string = strval($ret['0D7EA7F06E07F56780D733F18F46DDBB826DCB65']['seeders']);
However that odd string is also the first index of the array so you could also do it like this:
$string = strval($ret[0]['seeders']);
or if you want ot use only indexes ('seeders' is also the first index of the second array):
$string = strval($ret[0][0]);
if you just want the number then it's easy too:
$num = $ret[0][0];
It's not clear if you're looking to assign the array value(s?) as a separate variable(s?) or just to cast it into a string. Here's a nice way to accomplish all the above options, by assigning each array key as a separate variable with the matching array value:
$ret_vars = array_pop($ret);
foreach ($ret_vars as $variable_name=>$variable_value) :
${$variable_name} = (string)$variable_value;
endforeach;
In your original example, this would end up populating $seeders, $completed, $leechers and $infohash with their matching string values. Of course, make sure these variable names are not used/needed elsewhere in the code. If that's the case, simply add some sort of unique prefix to the ${} construct, like ${'ret_'.$variable_name}
There is a string variable containing number data , say $x = "OP/99/DIR"; . The position of the number data may change at any circumstance by user desire by modifying it inside the application , and the slash bar may be changed by any other character ; but the number data is mandatory. How to replace the number data to a different number ? example OP/99/DIR is changed to OP/100/DIR.
$string="OP/99/DIR";
$replace_number=100;
$string = preg_replace('!\d+!', $replace_number, $string);
print $string;
Output:
OP/100/DIR
Assuming the number only occurs once:
$content = str_replace($originalText, $numberToReplace, $numberToReplaceWith);
To change the first occurance only:
$content = str_replace($originalText, $numberToReplace, $numberToReplaceWith, 1);
Using regex and preg_replace
$x="OP/99/DIR";
$new = 100;
$x=preg_replace('/\d+/e','$new',$x);
print $x;
The most flexible solution is to use preg_replace_callback() so you can do whatever you want with the matches. This matches a single number in the string and then replaces it for the number plus one.
root#xxx:~# more test.php
<?php
function callback($matches) {
//If there's another match, do something, if invalid
return $matches[0] + 1;
}
$d[] = "OP/9/DIR";
$d[] = "9\$OP\$DIR";
$d[] = "DIR%OP%9";
$d[] = "OP/9321/DIR";
$d[] = "9321\$OP\$DIR";
$d[] = "DIR%OP%9321";
//Change regexp to use the proper separator if needed
$d2 = preg_replace_callback("(\d+)","callback",$d);
print_r($d2);
?>
root#xxx:~# php test.php
Array
(
[0] => OP/10/DIR
[1] => 10$OP$DIR
[2] => DIR%OP%10
[3] => OP/9322/DIR
[4] => 9322$OP$DIR
[5] => DIR%OP%9322
)
So here is the string that im scraping a page to read (using file get contents)
<th>Kills (K)</th><td><strong>4,751</strong></td><td><strong>0</strong></td>
How can i navigate to the above section of the page contents, and then isolate the 4,751 inside the above html and load it into $kills ?
Difficulty: the number will change and have additional numbers before the comma
Ok got it to work by removing all spaces and turning the page contents into a string
<?
$url = "http://combatarms.nexon.net/Community/Profile.aspx?user=tect0n";
$raw = file_get_contents($url);
$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
$content = str_replace($newlines, "", html_entity_decode($raw));
preg_match_all('|<th>.*?</th><td><strong>(\d+,\d+)</strong></td>|', $content,$match);
?>
This returns
Array ( [0] => Array ( [0] => Kills (K)4,751 [1] => Deaths (D)4,868 ) [1] => Array ( [0] => 4,751 [1] => 4,868 ) )
This should do it:
if (preg_match("/<th>Kills \(K\)<\/th><td><strong>([\d,]+)<\/strong>/",
$string, $matches)) {
$kills = str_replace(",","",$matches[1]);
} else {
$kills = 0;
}
This is what im using and gnarf's code returns 0
RageZ's returned an empty array
<?
$string = file_get_contents("http://combatarms.nexon.net/Community/Profile.aspx?user=tect0n");
if (preg_match("/<th>Kills \(K\)<\/th><td><strong>([\d,]+)<\/strong>/",
$string, $matches)) {
$kills = str_replace(",","",$matches[1]);
} else {
$kills = 0;
}
echo $kills;
?>
Coming up 0
here you go
preg_match_all('|<th>.*?</th><td><strong>([\d,]+)</strong></td>|x', $subject,$match);
var_dump($match);
but if I were you I would use xpath it's is safer.
preg_match_all('#\(K\).*?<strong>(.*?)</strong>#s',$html,$matches);
tell me that aint pretty
preg_match('#<table class="tbl_profile">(.*?)</table>#s',file_get_contents('http://combatarms.nexon.net/Community/Profile.aspx?user=tect0n'),$m);
preg_match_all('#<tr>.*?<t.*?>(.*?)</t.*?>.*?<t.*?>(.*?)</t.*?>.*?<t.*?>(.*?)</t.*?>.*?</tr>#s',preg_replace('#(<strong>)|(</strong>)|(<!--.*?-->)#s','',$m[1]),$r);
echo 'You got '.$r[2][1].' killz';
//print_r($r);
now tell me thaaaaaaat aint pretty, cooooool it.