I wrote a function to strip parameters from urls, the function looks like this
function remove_it($c_link){
$regex = array();
$award = array();
$regex[] = '/[\?&](?<name>sa)=(?<value>[^&=]+)/';
$regex[] = '/[\?&](?<name>ei)=(?<value>[^&=]+)/';
$regex[] = '/[\?&](?<name>ved)=(?<value>[^&=]+)/';
$regex[] = '/[\?&](?<name>usg)=(?<value>[^&=]+)/';
foreach($regex as $remove){
$c_link = preg_replace($remove,'',$c_link);
}
return $c_link;
}
When I use a testurl like this
$test = 'http://forum.gofeminin.de/forum/dietetique/__f2955_dietetique-Diatpillen.html&sa=U&ei=8doOUa6HOsfKtAaDpICIBQ&ved=0CB0QFjAA&usg=AFQjCNEcFS48QvteNkSNcszXv5RG6VUe2g';
It's woking perfect. Now I wanted to use it in my code. So I called to function with my data and it doesn't affect the string. I used print_r to see if the string looks strange, but it's just 1:1 like in $test
$TEST-> http://forum.gofeminin.de/forum/dietetique/__f2955_dietetique-Diatpillen.html&sa=U&ei=C9wOUZuvCoeQtQavpoHoDg&ved=0CB0QFjAA&usg=AFQjCNHkRBKRpZXZX7idJ6YmSG0AIxtOdw
print_r-> http://forum.gofeminin.de/forum/dietetique/__f2955_dietetique-Diatpillen.html&sa=U&ei=C9wOUZuvCoeQtQavpoHoDg&ved=0CB0QFjAA&usg=AFQjCNHkRBKRpZXZX7idJ6YmSG0AIxtOdw
As I used all debugging methods that I know of, I don't really know where I should start searching... any pointers ?
I made antoher testrun, and saved all data in an array, later on I wanted to stript the parameter for 1 url. Here the testcode:
echo '<pre>';
print_r($test).'</br>';
echo remove_it($test[0]);
echo '</pre>';
break;
the output was like :
Array
(
[0] => http://forum.gofeminin.de/forum/dietetique/__f2955_dietetique-Diatpillen.html&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CDUQFjAA&usg=AFQjCNGgMS-nHM2JY_PkIt7C_RT2dr9bUw
[1] => http://www.fitforfun.de/abnehmen/gesund-essen/diaetpillen/diaetpillen-appetitzuegler_aid_2100.html&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CEEQFjAB&usg=AFQjCNG60KJy3wLR8DnLm9gKQEn-uR6l3w
[2] => http://www.stern.de/ernaehrung/uebergewicht-abnehmen/diaetpillen-check-welche-mittel-machen-duenn-das-abc-der-schlankmacher-615772.html&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CEYQFjAC&usg=AFQjCNGLzi5UMG4g5INDkeBdMpENgY4gHg
[3] => http://getslim.de/diaetpillen-im-test&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CEoQFjAD&usg=AFQjCNEcZnpSlVVxLgskK9DfhBF9AHGC2w
[4] => http://www.br.de/fernsehen/bayerisches-fernsehen/sendungen/gesundheit/themenuebersicht/medizin/schlankheitspillen-diaet-tabletten100.html&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CFQQFjAE&usg=AFQjCNHujKjdfNsOkarYf6MwHCPODcISjw
[5] => http://www.diaetpillenvergleich.de/beste-diatpillen/&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CFoQFjAF&usg=AFQjCNFBgbYjgutHJfp-eQztXTsKYk7rTw
[6] => http://www.diaetpillen-online.de/&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CF4QFjAG&usg=AFQjCNF083onO0rkMuQjY0tEIhhdSM4Igg
[7] => http://diaet.erdbeerlounge.de/Diaetpillen/&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CGIQFjAH&usg=AFQjCNFhNr-gsFxK1-vfjhnC1A5qQi1ZjQ
[8] => http://diaet.erdbeerlounge.de/abnehmen-forum/Diaetpillen-_t2698848s1&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CGcQFjAI&usg=AFQjCNHhHY3zUnJtwF6-HV-DbsxaVUFxsg
[9] => http://www.gutefrage.net/tag/diaetpillen/1&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CG0QFjAJ&usg=AFQjCNHPYODXZA1Sa2rs6ItnUWTOYkJj3w
)
http://forum.gofeminin.de/forum/dietetique/__f2955_dietetique-Diatpillen.html&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CDUQFjAA&usg=AFQjCNGgMS-nHM2JY_PkIt7C_RT2dr9bUw
I made the test array and it works for me. It seems that your code is fine and something else is wrong.
Try wrapping the function input in double quotes.
remove_it("$test[0]");
Related
I have the following array extracted from a CSV file.
$line = Array (
[0] => First
[1] => Last
[2] => 102338100053
[3] => https://url.com/SKnuDbowTveUsHXwMAnixg?t=kIMVJtQ
[4] => 48a9ee0d-ba30-4ef7-94b0-75f03009e2c6
[5] => 1436.75
[6] => 21.55125
)
I am trying to get the value of [2]
I extract it like this
$number2 = $line[2];
This is fine. I get the following response. 102338100053
When I try to extract the first 6 numbers from the variable using
$Identifier = substr($number2 ,0,6)
I only get 2 numbers: 10
if I use 12
$siteIdentifier = substr($number2 ,0,12)
I get: 102338
I found this confusing so I checked the variable with urlencode
echo urlencode($number2);
This is what I received: %001%000%002%003%003%008%001%000%000%000%005%003%00
I need to get just the number and I dont know what to do to get it, as I am searching for this in a database and its not finding it?
Can someone please assist?
You may try using gettype() which gives you the datatype of your variables. Then the rest must be pretty simple to understand. I used $i<6 condition because you've mentioned you want only the first six integers.
$i= 0;
foreach($line as $key => $value){
if(gettype($value) == integer){
if($i<6){
$ints[$i] = $value;
$i++;
}
}
}
I have the following variable:
$checkbox = implode(';', $_POST['product']);
$checkbox is equal to "Product Name;Price;Unit", how can I add a break after every line?
At the moment $checkbox is equal to:
ASFP4040;18.95;1;ASFP4048;21;1;ASGS100100;25.45;1
I need it to be like:
ASFP4040;18.95;1;
ASFP4048;21;1;
ASGS100100;25.45;1;
EDIT:
I am writing this to a .TXT file, \n shows as text and doesn't actually create a new line.
As I'm not sure, how your $_POST['products'] var looks like, you might like one of these options:
If you have everything in a single array element like this
Array
(
[0] => ASFP4040
[1] => 18.95
[2] => 1
[3] => ASFP4048
[4] => 21
[5] => 1
[6] => ASGS100100
[7] => 25.45
[8] => 1
)
you could split the array into chunks and join them together
$data = implode("\n", array_map(function($chunk) {
return implode(';', $chunk);
}, array_chunk($_POST['product'], 3)));
Alternatively, if you have an array of strings like below:
Array
(
[0] => ASFP4040;18.95;1
[1] => ASFP4048;21;1
[2] => ASGS100100;25.45;1
)
a simple implode would be enough
$data = implode("\n", $_POST['product']);
Try this:
echo "'".implode("','",$checkbox)."'<br>";
You can use regular expressions to do this. Just replace my $str with your $checkbox.
$str = 'ASFP4040;18.95;1;ASFP4048;21;1;ASGS100100;25.45;1';
$str2 = preg_replace('/((?:(?:[^;]+);){3})/',"$1\n",$str);
echo $str2;
As explained in Magnus Eriksson's comment and mine, you just have to use "\n" as first parameter of your implode:
$checkbox = implode("\n", $_POST['product']);
Please notice the use of double quotes (") in order for \n to be used as a linebreak.
I am trying to program a web crawler but I have no idea, how to create a recursion for parsing a webpage and adding all the endresults into a final array.
I never worked with php before but I did alot of research on the internet and figured already out, how to parse the page I want to scrape.
Please note, that I have changed the $url value and the array result below to some values which I have randomly generated in my mind.
<?php
include_once "simple_html_dom.php"; //http://simplehtmldom.sourceforge.net/
$url = "https://www.scrapesite.com/pagetoscrape/index.html";
function parseLink($link) {
$html = file_get_html($link);
$html = $html->find("/html/body/script[2]/text", 0);
preg_match('/\{(?:[^{}]|(?R))*\}/', $html, $matches); //this regex extracts a json array
$json = json_decode($matches[0]);
$data = ($json->props->contents);
return $data;
}
function getFolders($basepath, $data) {
$data = $data->folders;
$result = array();
foreach ($data as $value) {
$result[] = array("folder", $basepath . "/" . $value->filename, $value->href);
}
return $result;
}
$data = getFolders("", parseLink($url));
print_r ($data);
?>
This script works fine and it outputs the following:
Array
(
[0] => Array
(
[0] => folder
[1] => /1
[2] => https://www.scrapesite.com/pagetoscrape/sjdfi327943sad/index.html
)
[1] => Array
(
[0] => folder
[1] => /2
[2] => https://www.scrapesite.com/pagetoscrape/345fdsjjsdfsdf/index.html
)
[2] => Array
(
[0] => folder
[1] => /3
[2] => https://www.scrapesite.com/pagetoscrape/46589dsjodsiods/index.html
)
[3] => Array
(
[0] => folder
[1] => /4
[2] => https://www.scrapesite.com/pagetoscrape/345897dujfosfsd/index.html
)
[4] => Array
(
[0] => folder
[1] => /5
[2] => https://www.scrapesite.com/pagetoscrape/9dsfghshdfsds3/index.html
)
)
Now, the script should execute the getFolders function for every item in the above array. This may return another array of folder which should get parsed too.
And then I want to create a final array where all the folders ABSOLUTE paths ($basepath . "/" . $value->filename) and href links are listed. I really appreciate every little hint.
I was able to find some example on the web but I can't figure out how to implement it here because I have almost no experience with programming languages in general.
Initialize an empty array and pass that as a reference to the getFolders() function. Keep putting the results of scraping inside this array. Also, you need to call getFolders() again inside the foreach loop of the getFolders(). Example below:
$finalResults = array();
getFolders("", parseLink($url), $finalResults);
Your getFolders() function signature will now look like below:
function getFolders($basepath, $data, &$finalResults) //notice the & before the $finalResults used for passing by reference
And, your foreach loop:
foreach ($data as $value) {
$finalResults[] = array("folder", $basepath . "/" . $value->filename, $value->href);
getFolders("", parseLink($value->href), $finalResults);
}
Above code is just an example. Change it according to your needs.
I've searched around and I found some similar questions asked, but none that really help me (as my PHP abilities aren't quite enough to figure it out). I'm thinking that my question will be simple enough to answer, as the similar questions I found were solved with one or two lines of code. So, here goes!
I have a bit of code that searches the contents of a given directory, and provides the files in an array. This specific directory only has .JPG image files named like this:
Shot01.jpg
Shot01_tn.jpg
so on and so forth. My array gives me the file names in a way where I can use the results directly in an tag to be displayed on a site I'm building. However, I'm having a little trouble as I want to limit my array to not return items if they contain "_tn", so I can use the thumbnail that links to the full size image. I had thought about just not having thumbnails and resizing the images to make the PHP easier for me to do, but that feels like giving up to me. So, does anyone know how I can do this? Here's the code that I have currently:
$path = 'featured/';
$newest = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS));
$array = iterator_to_array($newest);
foreach($array as $fileObject):
$filelist = str_replace("_tn", "", $fileObject->getPathname());
echo $filelist . "<br>";
endforeach;
I attempted to use a str_replace(), but I now realize that I was completely wrong. This returns my array like this:
Array
(
[0] => featured/Shot01.jpg
[1] => featured/Shot01.jpg
[2] => featured/Shot02.jpg
[3] => featured/Shot02.jpg
[4] => featured/Shot03.jpg
[5] => featured/Shot03.jpg
)
I only have 3 images (with thumbnails) currently, but I will have more, so I'm also going to want to limit the results from the array to be a random 3 results. But, if that's too much to ask, I can figure that part out on my own I believe.
So there's no confusion, I want to completely remove the items from the array if they contain "_tn", so my array would look something like this:
Array
(
[0] => featured/Shot01.jpg
[2] => featured/Shot02.jpg
[4] => featured/Shot03.jpg
)
Thanks to anyone who can help!
<?php
function filtertn($var)
{
return(!strpos($var,'_tn'));
}
$array = Array(
[0] => featured/Shot01.jpg
[1] => featured/Shot01_tn.jpg
[2] => featured/Shot02.jpg
[3] => featured/Shot02_tn.jpg
[4] => featured/Shot03.jpg
[5] => featured/Shot03_tn.jpg
);
$filesarray=array_filter($array, "filtertn");
print_r($filesarray);
?>
Just use stripos() function to check if filename contains _tn string. If not, add to array.
Use this
<?php
$array = Array(
[0] => featured/Shot01.jpg
[1] => featured/Shot01_tn.jpg
[2] => featured/Shot02.jpg
[3] => featured/Shot02_tn.jpg
[4] => featured/Shot03.jpg
[5] => featured/Shot03_tn.jpg
)
foreach($array as $k=>$filename):
if(strpos($filename,"_tn")){
unset($array[$k]);
}
endforeach;
Prnt_r($array);
//OutPut will be you new array removed all name related _tn files
$array = Array(
[0] => featured/Shot01.jpg
[2] => featured/Shot02.jpg
[4] => featured/Shot03.jpg
)
?>
I can't understand what is the problem? Is it required to add "_tn" to array? Just check "_tn" existence and don't add this element to result array.
Try strpos() to know if filename contains string "_tn" or not.. if not then add filename to array
$path = 'featured/';
$newest = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS));
$array = iterator_to_array($newest);
$filesarray = array();
foreach($array as $fileObject):
// Check - string contains "_tn" substring or not
if(!strpos($fileObject->getPathname(), "_tn")){
// Check - value already exists in array or not
if(!in_array($fileObject->getPathname(), $filesarray)){
$filesarray[] = $fileObject->getPathname();
}
}
endforeach;
print_r($filesarray);
Hello can someone help me with this regex please
here is my $lang_file:
define(words_picture,"Снимка");
define(words_amount,"бр.");
define(words_name,"Име");
define(words_price_piece,"Ед. цена");
define(words_total,"Обща цена");
define(words_del,"Изтрий");
define(words_delivery,"Доставка,но няма");
this is my code :
$fh = fopen($lang_file, 'r');
$data = str_replace($rep,"",fread($fh, filesize($lang_file)));
fclose($fh);
preg_match_all('/define\((.*?)\)/i', $data,$defines,PREG_PATTERN_ORDER);
when i print $defines i get this :
[0] => words_picture,"Снимка"
[1] => words_amount,"бр."
[2] => words_name,"Име"
[3] => words_price_piece,"Ед. цена"
[4] => words_total,"Обща цена"
[5] => words_del,"Изтрий"
[6] => words_delivery,"Доставка" //here is the part that is missing and i need it :-)
so when there is a comma inside the string it breaks the string there, and doesn't return correct value.
Try (koko.*?) as the match. That'll return koko for koko,goko. If you want it to return koko,goko, remove the ?. Make it (koko.*). That will return koko,goko for koko,goko.
Here's a site that I use to test my regex against a number of cases:
http://www.cyber-reality.com/regexy.html
based on your edit I'd say you're looking for (koko.*). If your code worked for everything else, use this:
preg_match_all('\(/define.*)\)/i', $data,$defines,PREG_PATTERN_ORDER);