I'm assuming the easiest way to do this will be with regex, but I just can't seem to find clear information on regex. I'm a beginner and all the information I'm finding is confusing.
I need to find every image in an HTML file, insert the folder extension, and then overwrite the file. I know how to do everything but the replacement. From my understanding, the code should look something like this:
preg_replace("^\"(.jpg|.jpeg|.gif|.png)$"....)
But I don't understand where to go from there. I need to keep the original value of whatever is between those things and add something to the beginning of it, so for example "image.jpg" would become "images/image.jpg".
$img = "<a href=\"hello.jpg\" /><a href=\"asdf.png\" /><a href=\"xkcd.gif\" />";
$img = preg_replace("/\"(\w+\.(jpg|jpeg|gif|png))\"/","\"images/$1\"",$img);
echo $img;
Output: <a href="images/hello.jpg" /><a href="images/asdf.png" /><a href="images/xkcd.gif" />
The regex can be improved using lookarounds, but I think they are overkill (and will make it more complex).
Related
I'm new to PHP and coding in general so apologies for this likely silly question. I know the answer will be extremly simple but try and I might, I just can't see it.
I'm trying to pull image path data from my database and concatenate with the code below so that I can enventually display it on my site using <?php print.... ?>
I have successfully done this. The problem I have now is setting the size of this image.
Please see my code below.
$Image_Path .= " <img src = db_images_product/".'$row['ImagePath']'." ".'height="100"'."/> ";
I will be indebted to anybody who can help on this.
I have researched this question and came across some answers but just could not make them work with my problem.
I's just the image size I have an issue with, nothing else.
I ended up using the code below and then using CSS. This was inspired by #Ndianz's answer.
$ImagePath = " <img src = db_images_product/".$row['ImagePath']." ".'Class="ProductImage"'."/> ";
I have ran all this code through an editor and it runs fine with no errors.
To illustrate concatenation here is a link with many scenario and explenations to help you,
Click Here
This is the way you currently have it,
$Image_Path .= " <img src = db_images_product/".'$row['ImagePath']'." ".'height="100"'."/> "
This is the way you would write that,
$Image_Path = '<img src="db_images_product/"'.$row['ImagePath'].'" height="100"/>';
I suggest you do this,
$Image_Path = '<img src="db_images_product/'.$row['ImagePath'].'">';
Then in your css file add the style to your img tag like this,
img {
height: 100px; //or whatever you want it to be.
}
You can use getimagesize() to grab the width and height then set the property on the image tag. the $row['ImagePath'] needs to be reachable by the script so you might have to play with this and add some ../ to get to the relative path of the image.
list($width, $height) = getimagesize($row['ImagePath']);
$Image_Path .= "<img src='db_images_product/{$row['ImagePath']}' height='{$height}' >";
You've just got some syntax errors. When you concatenate be careful about where your quote marks go. I'd use single quotes to open and close your PHP string so as not to conflict with the double quotes in your HTML. So pay close attention -- the single quote closes the PHP string right before the dot concatenates the $row variable. Then after the variable you add another dot to concatenate the end of the PHP string.
$Image_Path .= '<img src="db_images_product/"'.$row['ImagePath'].'" height="100"/>';
I'm trying via php to pull all images from a directory and print them as < li < img src="url.. etc...
After doing a bit of research on SO, i found that the glob function should serve this purpose well, but i can't get it to work unfortunately. I think the problem is with me trying to echo the ID in the path? but being a bit of a newbie i can't seem to see my error.
<?
$dirname="images/companies/";
echo '$current['id']';
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
?>
You have no dir separator between $dirname."*.jpg", so unless your $current['id'] ends in a slash, glob won't work.
Try replacing $dirname."*.jpg" with $dirname."/*.jpg".
Also: your $dirname is relative to the current PHP working directory. To make sure that this works regardless of where PHP was called from, use $dirname=dirname(__FILE__)."/images/companies/".$current['id']
Make sure thar $dirname is relative to the current path or use absolute paths, e.g.:
$dirname = $_SERVER['DOCUMENT_ROOT']."/../some/path/images/companies/";
Make sure that your path is not missing a slash, it would if $current['id'] had no trailing slash, that you had to write:
$images = glob($dirname."/*.jpg");
Thaths the problem when oyu just steal code, but dont understand what it does.
If you want to scan a directory, you can use scandir(), which returns an array containing all the files it found, e.g.
$files = scandir("your/path/");
To show them, you simply use print_r(), its ugly and not formatted, but it does its purpose, if you want more, use echo and html/css:
echo "<pre>";
print_r($files);
echo "</pre>";
No finished code, think a bit for yourself.
echo '<td>'.$row_sv['name'].'</td>';
I don't want to use any target and changed it like this but it messed up my table
echo '<td><a href="'.$row_sv['website].'$row_sv['name'].'</a></td>';
something wrong?
To make such pieces clearer I prefer using templates. In your case that would be:
printf( '<td>%s</td>', $row_sv['website'], $row_sv['name'] );
No mess with the quotes and opening/closing tags.
You should use the following:
echo '<td>'.$row_sv['name'].'</td>';
You mixed up the quotes a bit:
echo '<td>'.$row_sv['name'].'</td>';
You deleted too much, and then messed up something that was ok to begin with.
Use:
echo '<td>'.$row_sv['name'].'</td>';
In addition to deleting too much, you also had $row_sv['website] instead of $row_sv['website'] which should've cause a parse error too (unless it was just a typo here).
In the future here, you could also paste the HTML output instead of saying "it messed up my table" -- it'll make it easier for you to see the problem as well as folks here, I am sure.
I'm trying to display an image file from a directory using a PHP echo command and an IMG tag.
Here is the code:
//These variables represent the file name extensions from a form element from a previous page
$bannerimg=$_POST["banimg"];
$adimage=$_POST["adimage"];
echo "<img src='imgdir/'".$bannerimg."/>";
When I echo out the file variables ($bannerimg and $adimage) I get the proper file name and extension.
In theory, will this work? If so, what is the proper syntax to handle that echo statement?
Thanks for all the help.
Dustin
Yes it would work, but you should have tested that already.
Alternative syntaxes to the echo statement that I find a little bit more readable would be:
echo "<img src='imgdir/{$bannerimg}/>";
echo "<img src='imgdir/$bannerimg/>";
You can read all about variable parsing in the manual, the first syntax is the complex one and the second the simple. I prefer the complex one as the end of the variable is clearly defined and you can use it for complex expressions, not just simple variables.
You're doing it right but you can use the following just to keep it clean.
echo "<img src='imgdir/$bannerimg' />";
It should work.
To Sandeep's comment, I would have gone the other way.
echo '<img src="imgdir/'.$bannerimg.'/>';
Using " means the parser needs to check to see if there is anything to evaluate.
I am developing a Facebook App in which I get some text from the Database and Display it on the Page.
The Problem is that I want to insert a line Break in the variable e-g
If I copy a Text from database and store it in a Variable..
Let say
$text="I love to walk";
I want to insert a line break after "to" how can i do that?
I had tried to store the text like this in html
"I love to <html> <br> </html> but that didn't worked..
Just suppose this is the Text ..may be next time the text is entirely Differnet having no "to" word.
Depends on if you want to create new line in code output, or in HTML
$nl = "\r\n";
$nl_html = "<br />";
That exmple you provided modify like this:
$lyrics = "I love to <br> but that didn't worked.."
To automatically add line break after some text, use preg_replace
$lyrics = preg_replace('/to /',"to<br />",$lyrics);
see http://php.net/manual/en/function.preg-replace.php
$new_str = str_replace('to', 'to <br />', $str, 1);
If you want to output the text in a html page, you need to make it
$text="I love to <br /> walk";
If you want to output it to a file you need to make it
$text="I love to\r\nwalk";
or
$text="I love to\rwalk"; depending on the OS on which you will be reading the file
Hi I was having issues with this just as you do earlier today (I am new to PHP).
The way I fixed this was as follows:
$format_text = nl2br($formatthis);
You would then refer to $format_text.
What it does is it keeps the line breaks.
However I am not quite sure what you mean with your OP, after re-reading it. I went by the topic and I answered it as best I could.
If you are having trouble let's say echoing html code then most definitely you are having trouble with escaping characters.
For instance:
echo "a href="something" /a this won't work.
echo "a href=\"something\" /a this will work, notice the .
you have two options either use preg_replace or use a variable to save the value
please see php documentation for further info