I'm making a script to display all images in a specific folder using PHP. However, when iterating through the for loop, my string holding the image destination (../images/uploads/imageName.png) is having the forward slashes removed (so in the actual image tag it looks like: .. images uploads imageName.php). The file destination displays correctly when echoing out the $num string. My script is displayed below:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$files = glob("../images/uploads/*.*");
for ($i = 0; $i < count($files); $i++) {
$num = $files[$i];
echo $num;
echo '<img src"'.$num.'" alt="random image">'." ";
}
Any help would be very much appreciated
Your missing something here:
echo '<img src"'.$num.'" alt="random image">'." ";
Such as the = between src and the value
echo '<img src="'.$num.'" alt="random image">'." ";
I am sure your image won't display without it regardless of what the path was....
forward slashes removed (so in the actual image tag it looks like: .. images uploads imageName.php). The file destination displays correctly when echoing out the $num string.
What your probably seeing is how the browser is breaking when trying to figure out the HTML tag. Because it's correct when you echoing out the $num string the value of the variable is correct, it's the tag that is breaking down. PHP is not just going to magically do something to the value of a variable from one line to the next.
Cheers!
this will add a slash if your link doesn't have any slash at the end
if(substr($yourlinkvariable, -1) !== "/") {
$yourlinkvariable .= "/";
}
Maybe try with str_replace() function like so :
$num = str_replace(' ', '/', $files[$i]);
This should replace spaces by slash
Related
I use this code:
<?php
$texthtml = '<p>test</p><br><p><img src="1.jpeg" alt=""><br></p><p><img src="2.png" alt=""><br><img src="3.png" alt=""></p>';
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $texthtml, $image);
echo $image['src'];
?>
However, when I test it, I get last image (3.png) from a string.
I want to know how can I do for get first image (1.jpeg) in a string.
Try:
preg_match('/<img(?: [^<>]*?)?src=([\'"])(.*?)\1/', $texthtml, $image);
echo isset($image[1]) ? $image[1] : 'default.png';
Regex is not suitable for html tags.
You can read about it here: RegEx match open tags except XHTML self-contained tags
I suggest DOM document if it's more complex than what you have shown here.
If it's not more complex than this I suggest strpos to find the words and "trim" it with substr.
$texthtml = '<p>test</p><br><p><img src="1.jpeg" alt=""><br></p><p><img src="2.png" alt=""><br><img src="3.png" alt=""></p>';
$search = 'img src="';
$pos = strpos($texthtml, $search)+ strlen($search); // find postition of img src" and add lenght of img src"
$lenght= strpos($texthtml, '"', $pos)-$pos; // find ending " and subtract $pos to find image lenght.
echo substr($texthtml, $pos, $lenght); // 1.jpeg
https://3v4l.org/48iiI
Ok, so I was tasked with created a gallery, using a sql table is not an option, so I am doing what I can. This is my code, wich works fine, but it generates a hidden character at the end of every imate.
<?php
$photos = file("/elements/photos.php");
for ($i = 0; $i < count($photos); $i++) {
$allimages .= $imagefile = '<img src="/elements/photos/'.$photos[$i].'">';};
?>
<?=$allimages?>
This is the code that it generates
<img src="/elements/photos/t/a_little_kitten.jpg
">
I have been unable to find what
this means, I believe it means "blank space" or "new line", but I cannot find it.
This is the code I have tried, but it does not work either.
$allimages = preg_replace('/\s\s+/', ' ', $allimages)
Please help.
Below is the php file I am pulling the image names from. There is no code in this file, just text.
a_little_kitten.jpg
black_cat.jpg
basket.jpg
Try rtrim link or trim to remove the whitespace. As I can see that there is a whitespace at the end of your a_little_kitten.jpg and black_cat.jpg file.

 represents a line feed. Maybe you can use str_replace()
ex: str_replace(array("\n", "\r"), '', $photos) before for loop.
I have a very strange Problem. I am trying since 3 hours to the the output from $ht. When i echo $ht the result is http://www.google.com. There is no problem. But when i do file_get_contents with the variable $ht there is no output. I tried already with curl also no ouput - 404....
foreach ($hashtweet[1] as $ht)
{
$html = file_get_contents($ht); //NOT WORKING
$html = file_get_contents("http://www.google.com"); // WORKING
echo $html;
//echo $ht; = CORRECT OUT OF $ht = http://www.google.com
}
You should use trim function:
$html = file_get_contents(trim($ht));
Probably some spaces or other white characters are at the beginning or at the end of string
EDIT
In your question you told us the $ht is http://www.google.com but in comment you told is it's google.com. If you want to get http://google.com you need to add http://. Otherwise it means that you need in current directory file with name google.com
EDIT2
So you should compare those 2 strings. Add inside your loop:
if (trim($ht) == 'http://www.google.com') {
echo "same strings<br />";
}
else {
echo "there is difference<br />";
}
to make sure those strings are or aren't the same. You told that your $ht return string(21) "http://www.gooogle.com" so it's impossible it doesn't work because string http://www.gooogle.com string is exactly 21 characters long. Don't you change $ht variable before running file_get_contents or after it inside the loop?
Probably a simple problem here, but I cannot find it.
I am exploding a string that was input and stored from a textarea. I use nl2br() so that I can explode the string by the <br /> tag.
The string explodes properly, but when I try to get the first character of the string in a while loop, it only returns on the first line.
Note: The concept here is greentexting, so if you are familiar with that then you will see what I am trying to do. If you are not, I put a brief description below the code sample.
Code:
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$comment = nl2br($row['comment']);
$sepcomment = explode("<br />", $comment);
$countcomment = count($sepcomment);
$i = 0;
//BEGIN GREENTEXT COLORING LOOP
while($i < $countcomment) {
$fb = $sepcomment[$i];
$z = $fb[0]; // Check to see if first character is >
if ($z == ">") {
$tcolor = "#789922";
}
else {
$tcolor = "#000000";
}
echo '<font color="' . $tcolor . '">' . $sepcomment[$i] . '</font><br>';
$i++;
}
//END GREENTEXT COLORING LOOP
}
Greentext: If the first character of the line is '>' then the color of that entire line becomes green. If not, then the color is black.
Picture:
What I have tried:
strip_tags() - Thinking that possibly the tags were acting as the first characters.
$fb = preg_replace("/(<br\s*\/?>\s*)+/", "", $sepcomment[$i]);
str_replace()
echo $z //Shows the correct character on first line, blank on following lines.
$z = substr($fb, 0, 1);
Here is a test I just did where I returned the first 5 characters of the string.
Any ideas for getting rid of those empty characters?
Try "trim" function
$fb = trim($sepcomment[$i]);
http://php.net/manual/en/function.trim.php
(probably line breaks are the problem, there are \n\r characters after tag)
I'm using PHP to get all the "script" tags from web pages, and then appending text after the </script> that is not always valid html. Because it's not always valid markup I can't just use appendchild/replacechild to add that information, unless I'm misunderstanding how replacechild works.
Anyway, when I do
$script_tags = $doc->getElementsByTagName('script');
$l = $script_tags->length;
for ($i = $l - 1; $i > -1; $i--)
$script_tags_string = $doc->saveXML($script_tags->item($i));
This puts "<![CDATA[" and "]]>" around the contents of the script tag. How can I disable this? Please don't tell me to just delete it afterwards, that's what I'm going to do if I can't find a solution for this.
I have a suspicion that the CDATA is inserted because it would otherwise be invalid XML.
Have you tried using saveHTML instead of saveXML?
One way I've found to fix this:
Before echoing the document, make a loop around all script tags, and use str_replace for "<", ">" to some string, make sure to only use that string inside script tags.
Then, use the method saveXML() in a variable, and finally use str_replace replacing "STRING" to "<" or ">"
Here is the code:
<?php
//First loop
foreach($dom->getElementsByTagName('script') as $script){
$script->nodeValue = str_replace("<", "ESCAPE_CHAR_LT", $script->nodeValue);
$script->nodeValue = str_replace(">", "ESCAPE_CHAR_GT", $script->nodeValue);
}
//Obtaining XHTML
$output = $dom->saveXML();
//Seccond replace
$output = str_replace("ESCAPE_CHAR_LT", "<", $output);
$output = str_replace("ESCAPE_CHAR_GT", ">", $output);
//Print document
echo $output;
?>
As you can see, now you are free to use "<" ">" in your scripts.
Hope this helps someone.