I'm trying to read a file and look for a specific element within the file. Once it finds it, I would like the script to add the element to a variable. So far I have not been able to find anything on this.
This is kind of what I'm looking for:
$var = read(($file), "<!--Everything in the comment line-->")
You should use file_get_contents(…) to read the full file at once and strpos() to locate the part of interest, or use a regex if it is something more abstract.
Related
I want to extract variable lengths of information from a jpeg-file using PHP, but it is not exif-data.
If I open the jpeg with a simple text editor, I can see that the wanted informations are at the end of the file and seperated by \00.
Like this:
\00DATA\00DATA00DATA\00DATA\000\00DATA
Now if I use PHP's file_get_contents() to load the file into a string, the dividers \00 are gone and other symbols show up.
Like so:
ÿëžDATADATADATADATADATA ÿÙ
Could somebody please eplain:
Why do the \00 dividers vanish?
How to get the informations using PHP?
EDIT
The question is solved, but for those seeking a smarter solution, here is the file I try to obtain the DATA parts from: https://www.dropbox.com/s/5cwnlh2kadvi6f7/test-img.jpg?dl=0 (yes I know its corrupted)
Use instead $data = exif_read_data("PATH/some.jpg") it will give you all headers data about image, you can check its manual here - http://php.net/manual/en/function.exif-read-data.php
I came up with a solution on my own. May not be pretty, but works for me.
Using urlencode(file_get_contents()) I was able to retrieve the \00 parts as %00.
So now it reads like this:
%00DATA%00DATA%00DATA%00DATA%000%00DATA
I can split the string at the %00 parts.
I am going to accept this answer, once SO lets me do so and nobody comes up with a better solution.
I created a test using PHP WebDriver and Selenium. Now I want to make sure that a certain text is contained in an element. How do I do that? I tried:
$web_driver->wait(3)->until(
WebDriverExpectedCondition::textToBePresentInElement(WebDriverBy::cssSelector('.cart-price span.price'), '55,00 €')
);
But this always ends in a TimeoutException. Isnt there a way to really use something like this:
assertTrue(WebDriverExpectedCondition::textToBePresentInElement(WebDriverBy::cssSelector('.cart-price span.price'), '55,00 €'))
Thanks for your help!
$web_driver->wait(3)->until(
WebDriverExpectedCondition::textToBePresentInElement(
WebDriverBy::cssSelector('.cart-price span.price'), '55,00 €'
)
);
is already the assertTrue to me. It throws the TimeoutException if it cannot find the element containing certain text within 3 seconds.
If you are seeing the text on the browser, it might happen that there is no element found by the WebDriverBy or there are more than one element and the driver is getting another element which match the WebDriverBy.
If you are using PHP, you might have to change the
max_execution_time
in your PHP.ini file.
This sets the number of seconds that a PHP script can run for.
Ok, I am working on a flatfile shoutbox, and I am trying to achieve a way to get the username from the flatfile and making it a variable so I can use it to make a call to the database to check if the user is admin so they can delete/ban users directly from the shoutbox.
This is an example line in the flatfile
<div><i><div class='date'>12/08/2012 18:56 pm </div></i> <div class='groupAdmin'><b>Admin</b></div><b>kira423:</b> hiya :D</div>
So I wanna take the username which is kira423 in this case and create a variable such as $shoutname and make it equal kira423
I have tried a google search and looked around on here, but was unable to find an answer, so I am hoping that I can get some insight on how to do this with a question of my own here.
Thanks,
Kira
You should use preg_match for those tasks like this:
preg_match_all('|<div class=\'date\'>(?P<date>.*?) .*<a.*>(?P<user>.*)</a>|i', $data, $matches);
var_dump($matches);
Interating through all array elements:
foreach ($matches['user'] as $key => $user) {
var_dump($user);
}
I think you should just parse each line in the flatfile as HTML (there are simple HTML tags used), just like described in PHP Parse HTML code (or type "php parse HTML" in google). Then you may access the username (kira123) from an array or whatever.
PS HTML is not the best way you can store messages to display. Even CSV seems to be better - it'd be "kira123;date;some text" - it's easier to read and to access each part. When displaying, use the standar decorator pattern.
In a Symfony project, I have the following php code inside a YML file. I was hoping this code would read another yml file and modify the content based upon its value. However, sfYaml::load is returning a string instead of an array. Any ideas why this might be?
In the following code, I expected $s to contain an array, but instead it contains a string with the value "../config/server_settings.yml".
From databases.yml:
dsn: mysql:host=myhost;dbname=mydbname<?php $s = sfYaml::load('../config/server_settings.yml');var_dump($s); ?>
It looks like it's treating your input as a string of yml content, possibly because it can't find the file. Try using the full path or some quick debugging with is_file('../config/server_settings.yml')
It looks like you're trying to define custom settings.
Perhaps this page might help you out:
http://trac.symfony-project.org/wiki/HowToCacheCustomConfiguration
I want to create a PHP script that grabs the content of a website. So let's say it grabs all the source code for that website and I say which lines of code I need.
Is there a function in PHP that allows you too do this or is it impossible?
Disclaimer: I'm not going to use this for any illegal purposes at all and not asking you too write any code, just tell me if its possible and if you can how I'd go about doing it. Also I'm just asking in general, not for any specific reason. Thanks! :)
file('http://the.url.com') returns an array of lines from a url.
so for the 24th line do this:
$lines = file('http://www.whatever.com');
echo $lines[23];
This sounds like a horrible idea, but here we go:
Use file_get_contents() to get the file. You cannot get the source if the web server first processes it, so you may need to use an extension like .txt. Unless you password protect the file, obviously anybody can get it.
Use explode() with the \n delimiter to split the source code into lines.
Use array_slice() to get the lines you need.
eval() the code.
Note: if you just want the HTML output, then ignore the bit about the source in step 1 and obviously you can skip the whole eval() thing.