Php parse string error - php

I am extracting files from a string which can be entered by a user or taken from reading a page source.
I want to extract all .jpg image URLs
So, I am using the following (example text shown) but a) it only returns the first one and b) it misses off '.jpg'
$word1='http://';
$word2='.jpg';
$contents = 'uuuuyyyyyhttp://image.jpgandagainhereitishttp://image2.jpgxxxxcccffff';
$between=substr($contents, strpos($contents, $word1), strpos($contents, $word2) - strpos($contents, $word1));
echo $between;
Is there maybe a better way to do this?
In the case of parsing a web page I cannot use a simple DOM e.g. $images = $dom->getElementsByTagName('img'); as sometimes the image references are not in standard tags

You can do something like this :
<?php
$contents = 'uuuuyyyyyhttp://image.jpgandagainhereitishttp://image2.jpgxxxxcccffff';
$matches = array();
preg_match_all('#(http://[^\s]*?\.jpg)#i',$matches);
print_r($matches);

You can either do this using preg_match_all (as previously answered) or alternatively use the following function.
It simply explodes the original string, checks all parts for a valid link and adds it to the array, that's getting returned.
function getJpgLinks($string) {
$return = array();
foreach (explode('.jpg', $string) as $value) {
$position = strrpos($value, 'http://');
if ($position !== false) {
$return[] = substr($value, $position) . '.jpg';
}
}
return $return;
}

Related

Find the number of pages in a PDF using PHP

I have researched this for a few days now and finally found something that seemed to work, but I am getting the wrong result. I need to count the number of pages in a PDF file on a remote server. My code opens the PDF, but it's not finding the correct number of pages and I'm not sure why.
Here is my code so far:
$CI = &get_instance();
$CI->load->library('Awss3', null, 'S3');
$CI->load->library('Pdflib');
$data = $CI->S3->readFile('uploads/225572/filename.pdf', false, 'bucket-name');
$needle = 'Page';
$positions = array();
$lastPos = 0;
while (($lastPos = strpos($data, $needle, $lastPos))!==false) {
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($needle);
}
echo count($positions);
foreach ($positions as $value) {
echo $value . '<br />';
}
$test = strpos($data, 'Page');
If I echo out the $data, I get lots of symbols, etc. and some words, but the $test comes out to 0 when it should be 16. Does it depend on the type of PDF or do I need to decode it or something like that?
Simplest of all is using ImageMagick
here is a sample code
$image = new Imagick();
$image->pingImage('myPdfFile.pdf');
echo $image->getNumberImages();
otherwise you can also use PDF libraries like MPDF or TCPDF for PHP

Php code that returns an array with filenames of files which contains a string

Im trying to make a Php file that receives nothing and checks every file on the folder, searching for a string inside them. it echos a array of filenames that have the string inside. Any way to do it, possibly with low memory usage?
Thank you a lot.
To achieve something like this, I recommend you read about the DirectoryIterator class, file_get_contents, and about strings in PHP.
Here is an example of how you can read the contents of a a given directory ($dir) and use strstr to search for a specific string occurrence in each file's contents ($contents):
<?php
$dir = '.';
if (substr($dir, -1) !== '/') {
$dir .= '/';
}
$matchedFiles = [];
$dirIterator = new \DirectoryIterator($dir);
foreach ($dirIterator as $item) {
if ($item->isDot() || $item->isDir()) {
continue;
}
$file = realpath($dir . $item->getFilename());
// Skip this PHP file.
if ($file === __FILE__) {
continue;
}
$contents = file_get_contents($file);
// Seach $contents for what you're looking for.
if (strstr($contents, 'this is what I am looking for')) {
echo 'Found something in ' . $file . PHP_EOL;
$matchedFiles[] = $file;
}
}
var_dump($matchedFiles);
There is some extra code in this example (adding a trailing slash to $dir, skipping dot files and directories, skipping itself, etc.) that I encourage you to read and learn about.
<?php
$folderPath = '/htdocs/stock/tae';
$searchString = 'php';
$cmd = "grep -r '$searchString' $folderPath";
$output = array();
$files = array();
$res = exec($cmd, $output);
foreach ($output as $line) {
$files[] = substr($line, 0, strpos($line, ':'));
}
print_r($files);

How to read a csv file with php code inside?

i searched Google but found nothing what fits for my problem, or i search with the wrong words.
In many threads i read, the smarty Template was the solution, but i dont wont use smarty because its to big for this little project.
My problem:
I got a CSV file, this file contents only HTML and PHP code, its a simple html template document the phpcode i use for generating dynamic imagelinks for example.
I want to read in this file (that works) but how can i handle the phpcode inside this file, because the phpcode shown up as they are. All variables i use in the CSV file still works and right.
Short Version
how to handle, print or echo phpcode in a CSV file.
thanks a lot,
and sorry for my Bad english
Formatting your comment above you have the following code:
$userdatei = fopen("selltemplate/template.txt","r");
while(!feof($userdatei)) {
$zeile = fgets($userdatei);
echo $zeile;
}
fclose($userdatei);
// so i read in the csv file and the content of csv file one line:
// src="<?php echo $bild1; ?>" ></a>
This is assuming $bild1 is defined somewhere else, but try using these functions in your while loop to parse and output your html/php:
$userdatei = fopen("selltemplate/template.txt","r");
while(!feof($userdatei)) {
$zeile = fgets($userdatei);
outputResults($zeile);
}
fclose($userdatei);
//-- $delims contains the delimiters for your $string. For example, you could use <?php and ?> instead of <?php and ?>
function parseString($string, $delims) {
$result = array();
//-- init delimiter vars
if (empty($delims)) {
$delims = array('<?php', '?>');
}
$start = $delims[0];
$end = $delims[1];
//-- where our delimiters start/end
$php_start = strpos($string, $start);
$php_end = strpos($string, $end) + strlen($end);
//-- where our php CODE starts/ends
$php_code_start = $php_start + strlen($start);
$php_code_end = strpos($string, $end);
//-- the non-php content before/after the php delimiters
$pre = substr($string, 0, $php_start);
$post = substr($string, $php_end);
$code_end = $php_code_end - $php_code_start;
$code = substr($string, $php_code_start, $code_end);
$result['pre'] = $pre;
$result['post'] = $post;
$result['code'] = $code;
return $result;
}
function outputResults($string) {
$result = parseString($string);
print $result['pre'];
eval($result['code']);
print $result['post'];
}
Having PHP code inside a CSV file that should be parsed and probably executed using eval sounds pretty dangerous to me.
If I get you right you just want to have dynamic parameters in your CSV file right? If thats the case and you don't want to implement an entire templating language ( like Mustache, Twig or Smarty ) into your application you could do a simple search and replace thing.
$string = "<img alt='{{myImageAlt}}' src='{{myImage}}' />";
$parameters = [
'myImageAlt' => 'company logo',
'myImage' => 'assets/images/logo.png'
];
foreach( $parameters as $key => $value )
{
$string = str_replace( '{{'.$key.'}}', $value, $string );
}

php script to search multiple webpages from file for specific word

First excuse me for the bad english.
I am trying to build a php script to search multiple webpages from a .txt file for specific word.
More specific:
I have a .txt file where i have stored many urls (every url is on one line, so if i have 10 urls the file have 10 lines) and i want the script to check the webpage content of each url for a specific word. So if the word is found on the webpage the script will return ONLINE othewise will return DOWN.
I build the script but the problem is that it always return ONLINE even if the url from file doesn't have the specific word in it's webpage content.
<?php
$allads = file("phpelist.txt");
print("Checking urls: <br><br><br><strong>");
for($index = 0; $index <count($allads); $index++)
{
$allads[$index] = ereg_replace("\n", "", $allads[$index]);
$data = file_get_contents('$allads[$index]');
$regex = '/save/';
if (preg_match($regex, $data)) {
echo "$allads[$index]</strong>...ONLINE<br><strong>";
} else {
echo "$allads[$index]</strong>...DOWN<br><strong>";
}
}
print("</strong><br><br><br>I verified all urls from file!");
?
To search the particular webpage for a given string, I'd use stripos() (case-insensitive) or strpos() (case-sensitive) instead of regular expressions:
if( stripos(haystack, needle) !== FALSE ) {
//the webpage contains the word
}
An example:
$str = 'sky is blue';
$wordToSearchFor = 'sky';
if (strpos($str, $wordToSearchFor) !== false) {
echo 'true';
}
else {
echo 'Uh oh.';
}
Demo!
Although, programmitcally skimming through webpages isn't considered a good practice and shouldn't be done unless it's absolutely necessary.
UPDATE:
In your file_get_contents call you're doing:
$data = file_get_contents('$allads[$index]');
You're using single quotes, and the variable values do not get replaced. You'll have to use double quotes to have file_get_contents fetch the actual URL. Replace it with:
$data = file_get_contents("$allads[$index]");
Another thing I noticed is that you're using the deprecated ereg_replace() function in your code. See the red box? Relying on depreacted functions are highly discouraged.
Your code, after all the above corrections, should look like:
$allads = file("phpelist.txt");
print("Checking urls: <br><br><br><strong>");
for($index = 0; $index <count($allads); $index++)
{
$allads[$index] = str_replace("\n", "", $allads[$index]);
$data = file_get_contents("$allads[$index]");
$searchTerm = 'the';
if (stripos($data, $searchTerm) !== false) {
echo "$allads[$index]</strong>...ONLINE<br><strong>";
}
else
{
echo "$allads[$index]</strong>...DOWN<br><strong>";
}
}
print("</strong><br><br><br>I verified all urls from file!");
?>

PHP file to open and modify other php (With find and replace via regex?)

I want to do the following
I want to create .php file (executed via cronjobs) that will paste this code $files[] = 'example.php';
to other php file (paste.php) but it has to find the lastest $files[] line like regex $files[] = '(AnythingHere)'; and after this line to paste the new line. It can have random number of pages so I have no way of knowing.
<?php
if (!isset($php_file)) {
$files[] = 'page1.php';
$files[] = 'page2.php';
$files[] = 'page3.php';
$files[] = 'page4.php';
$file = $files[ rand(0,count($files)) ];
I hope you guys understand what I want; can anyone help me out with this one?
if you have ONLY $file[] = '...' in paste.php, you can simply append to the file:
$line = '$file[] = "pageX.php";' . PHP_EOL;
file_put_contents('paste.php', $line, FILE_APPEND);
of you want the last "page[]" enty.
$yourNewLine = '$file[] = "pageX.php";'; // this is an example. put your "line" prm here
$filename = 'paste.php';
$lines = file($filename);
$lines = array_reverse($lines)
$found = false;
$i = 0;
while ( ! $found )
{
if ( strpos($lines[$i], '$files[] = ' === 0) )
{
$found = true;
array_splice($lines, $i, 0, $yourNewLine.PHP_EOL);
}
$i++;
}
$lines = array_reverse($lines);
file_put_contents($filename, $lines);
Instead of doing it this way, how about instead setting your files array in a script and then include it at the top. This way you can reference the array directly and still only have to edit the file listing in only one place.
Quick and dirty first-fit solution:
Open the file
Read each line until you find one matching your regex for $files[] = ...
Read more lines until you find one that doesn't match the regex
Write each line read in 2 and 3 to the output file
Insert your new line into the output
Write the rest of the input to the output
This may not be the best way to approach the problem, drawbacks being that you have to read each line in and compare it with your regex until you find your insertion point. You'll also probably have a temporary file for output which you'll then rename to the original filename.
You'll have 2 while loops:
while (line does not match): read next line
and then
while (line does match): read next line
Someone who knows PHP better than I do might be able to come up with something a bit cleaner, but if you're just looking for something quick to get the job done, this ought to work.
Having this code:
$filesArray = array('page1.php','page2.php','page3.php','page4.php','page5.php',);
then getting the php file with $data = file("path/to/editable_file.php");
foreach($data as $line)
{
if(preg_replace("/\$filesArray\s=\sarray\([\w'.,]+()\);/", "'".$newfilename."',", $line, $match))
{
file_put_contents(implode("\r\n", $data));
break;
}
}

Categories