Re: replacing HTML string data

From: Date: Thu, 13 Jul 2000 20:51:44 +0000
Subject: Re: replacing HTML string data
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-6532@lists.php.net to get a copy of this message
In article <396DDD6B.D7E87077@billtron.com>, bill@billtron.com (bill) wrote: something like this should work: $fp = fopen($file,"r"); if ($fp) { while (!feof($fp)) { $buffer = fgets($fp, 1024); if (eregi("<img[[:space:]]+src=\"([^.]+)\.([[:alpha:]]{3})\"",$buffer,$regs)) { $regs[1] = ereg_replace("[[:space:]]","_",$regs[1]); $regs[1] = rawurlencode($regs[1]); $regs[2] = strtolower($regs[2]); $buffer = eregi_replace("src=\"[^\"]+\"","src=\"".$regs[1].".".$regs[2]."\"",$buffer); } $newFileBuffer .= $buffer; } fclose($fp); (...do your new file open and write here). } file I/O should be done in the manner above to allow you to trap errors. Using the fgets(fread) with the optional size parameter allows you to read in files that might be too large to read in one pass. you can substitute the POSIX character classes ([:alpha:], etc) with something like [A-Za-z] or use perl compatible regex stuff with the preg functions if you have them compiled in. Jeff > How can I fix the image tags of an html file when it is uploaded? > > A client uses MS-Word to create newsletters with images and uploads them > to the server. > > Unfortunately, the images often have non-legitimate names (names with > spaces, etc.). > > When she uploads the images my upload script automatically fixes the > image names. But, the HTML tags in her newsletter are no longer > correct: > > <img src="bad filename.JPG" width, height, etc.> > > I can grab the text between the body tags as a string > > if (file_exists($file)) { > $fp = fopen($file,"r"); > $contents = fread($fp); > fclose($fp); > $newstext = ereg_replace(".+<body.*>","",$contents); > $newstext = ereg_replace("</body>.+","",$article); > } > > But how do I grab the image names from $newstext to perform replacements > on them? > > I need to > > replace spaces " " with underlines "_" > > urlencode the remaining name > > and make the extension (JPG or GIF) to be all lowercase. > > Possible? > > thanks, > > bill > >

« previous php.general (#6532) next »