Forum Moderators: coopster
--
preg_replace -- Perform a regular expression search and replace
Description
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"
with
<img src=""
You could also use preg_replace() to replcae the entire image tag with just the alt-text.
This one will be better.
$new = preg_replace(
'/<img (.*?)src="[a-z0-9.:\/\/]+"(.*?)>/i',
'<img $1src=""$2>',
$Row[body]
);
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.
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>");
break;
default:
print (" ");
break;
}
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]
print("some fine text here, maybe with an <img src=\"hi.jpg\" alt=\"image\">");
with:
$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:
require("latestproject.php");
echo $latestproject;
or
echo preg_replace(pattern, replacement, $latestproject);
$lineoftext =~ s/\<img.*?alt=\"(.*?)\".*?\>/\[$1\]/gi;
Sorry it's perl, should work in PHP also:
preg_replace('\<img.*?alt=\"(.*?)\".*?\>','\[$1\]',$lineoftext)
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)
<?
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 you can use this condition to read and strip the image tags from a file to be included with the methods posted above.
<?php
$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?
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.