homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

stop images loading
how to stop images loading in text version of site?

 9:47 am on Jul 27, 2004 (gmt 0)

I want to stop images loading and instead to have the 'alt' tag of each image to be displayed. this is for a text only version of the site, how can it be done?



 9:59 am on Jul 27, 2004 (gmt 0)

AFAIK, there is no server-side command to stop the loading of images. Maybe you can send an empty src-attribute:

<img src="" alt="hi there" width=100 height=100 border=1>


 10:17 am on Jul 27, 2004 (gmt 0)

yea i thought of that but the data is coming for a database that is shared between the text only and graphic site.


 10:29 am on Jul 27, 2004 (gmt 0)

You can 'strip' the image tag with a nice litlle regexp, like

$output = preg_replace('/<img src="[a-z0-9.]+"/i', '<img src=""', $fromdatabase);

(not tested)


 11:39 am on Jul 27, 2004 (gmt 0)

could you explain this a bit please?


 12:52 pm on Jul 27, 2004 (gmt 0)

preg_replace is an advanced method to replace pieces in a string with something else. From the manual:


preg_replace -- Perform a regular expression search and replace
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit])

Searches subject for matches to pattern and replaces them with replacement. If limit is specified, then only limit matches will be replaced; if limit is omitted or is -1, then all matches are replaced.


The script I posted replaces
<img src="anything.ext"
<img src=""

You could also use preg_replace() to replcae the entire image tag with just the alt-text.


 4:35 pm on Jul 27, 2004 (gmt 0)

ok, but the variable $output is coming from a database, how can i link it?


 9:55 pm on Jul 27, 2004 (gmt 0)

> $output = preg_replace('/<img src="[a-z0-9.]+"/i', '<img src=""', $fromdatabase);

$fromdatabase is the original string.
$output contains the output of the replace action.


 1:22 am on Jul 28, 2004 (gmt 0)

$new = preg_replace('/<img src="[a-z0-9.]+"/i', '<img src=""', $Row[body]);

print ("$new");

this is what i had, but it didnt work!


 8:22 am on Jul 28, 2004 (gmt 0)

Ah yes, that's why I said: not tested ;)

This one will be better.

$new = preg_replace(
'/<img (.*?)src="[a-z0-9.:\/\/]+"(.*?)>/i',
'<img $1src=""$2>',

I tested it on the source of this page, and it works fine. It also works with stuff where the src-attribute is not the first one, like <img border="0" alt="bla" src="pic.png">.

Take my advice and spend some time on regular expressions. Methods like preg_replace() offer a very powerful tool for advanced search and replace actions. I couldn't live without them ;). www regular-expressions dot com may be a good starting point.


 7:48 pm on Jul 29, 2004 (gmt 0)

yea, ive invested in a php book! but ive only been using php for about 2 weeks now!

ok heres another one for you,

heres a switch control (i prefer these to if expressions!),

switch ($Row[pgname])

case "index":
print ("<p><b>Latest News:</b></p>");

require ("news.php");

print ("<p><b>Our Latest Project: </b></p> <p align=\"center\"><b>");

require ("latestproject.php");

print ("</b></p>");


print (" ");


if want to strip the file latestproject.php of all images. how can i do this using your method?

I only want it striped in this switch cos this switch will be used in the text only version of my site!

oh and thanks for ur help so far!

[edited by: jatar_k at 7:53 pm (utc) on July 29, 2004]
[edit reason] no personal urls thanks [/edit]


 8:29 am on Jul 30, 2004 (gmt 0)

preg_replace() requires a string as input, so you'll need to have latestproject.php produce a string. I guess that right now that script contains echo and print statements, which send the output immediately to the browser. So you should replace the echo/print statements:

print("some fine text here, maybe with an <img src=\"hi.jpg\" alt=\"image\">");


$latestproject .= "some fine text here, maybe with an <img src=\"hi.jpg\" alt=\"image\">";

$latestproject is just a name that seems handy because it's easy to remember. Once you have included the file into the other one, $latestproject will be available for further handling:

echo $latestproject;


echo preg_replace(pattern, replacement, $latestproject);


 8:40 am on Jul 30, 2004 (gmt 0)

You want the alt tag displaying?

$lineoftext =~ s/\<img.*?alt=\"(.*?)\".*?\>/\[$1\]/gi;

Sorry it's perl, should work in PHP also:


Needs checking - never done PHP before!

This matches <img...alt="..."...>
The alt bit matching is in brackets (can be used in the replace string as $1).
The replacement string puts the alt tag ($1) into square brackets.

So <img src='picture.jpg' width='24' alt='Logo'> becomes [Logo] (and the image tag is lost altogether)


 9:01 am on Jul 30, 2004 (gmt 0)

Why not just use a different stylesheet? Eg:

img {display:none;}

There's also a way to show alt text but it don't work in IE6.


 10:23 pm on Jul 30, 2004 (gmt 0)

These are all good solutions, but if I am not reading incorreclty, Ahmed needs no graphics in a text version, right?
Then you need to add a condition to your pages, so you can decide what to do in each version.
I think something like:

if ($version!="text") {
echo "<img src=\"\" alt=\"Some image\">\n"; // or whatever you want/need
} else {
echo "<img src=\"image.jpg\" alt=\"Some image\">\n"; // show the image

Or did I just got it all wrong?

Or you can use this condition to read and strip the image tags from a file to be included with the methods posted above.


 8:47 pm on Jul 31, 2004 (gmt 0)

yea, thats kinda what i needed, but becuase all the data is being pulled out of a shared DB, i cant specify a version variable. I tried having a variable where...

$filename = $_SERVER["DOCUMENT_name"];

if ($filename)

blah blah...

thats an idea actually!

Thanks for the help! :D


 8:54 pm on Jul 31, 2004 (gmt 0)

and it works:

$filename = $_SERVER["DOCUMENT_NAME"];
if ($filename!="textnavi.php") {
echo "Kent Press Pictures\n"; // or whatever you want/need
} else {
echo "<img src=\"/images/clients/kpx/logo_03.gif\" alt=\"Kent Press Pictures\" width=\"280\" height=\"100\" border=\"0\">\n"; // show the image


thanks! :D

ps whats the diff between echo and print?


 9:06 am on Aug 1, 2004 (gmt 0)

no sorry, it didnt work! :'(


 11:03 am on Aug 1, 2004 (gmt 0)

I appreciate that this is for a text version, but why not simply leave the images in and allow the user to decide whether they want images on or off?
If they are using a text-reader, then images won't be supported so won't display anyway.
(alt and title attributes will be used to identify and describe the 'missing' image.)
If they are using a browser which supports (and is permitting) images, then that suggest that they (the user) do not object to seeing images, even on 'lite' pages.

If they really didn't want to see images, they could (and probably would) use the browser prefs to disable them.

I understand what you're trying to achieve, but I think you're over-thinking things and creating ad-hoc (and clumsy) solutions where better solutions already exist.

Don't bother trying to pre-empt their requirements by knobbling otherwise good markup.
The user already has the means to disable images if that's what they want.


 12:08 pm on Aug 1, 2004 (gmt 0)

thats true, but i always get the odd fool telling me:

"why has your text only site got graphics in it!?"

Get what i mean?

Ive abondoned that idea now and just gona stick to keepin the code clean so i can use it! thanks for all the help u guy!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved