str_replace rawurlencode apostrophes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

str_replace rawurlencode apostrophes

Post by mesz »

Code: Select all

<?php
 $line = str_replace("\r\n","<br>",$line); 
 $line = str_replace(rawurlencode(" "), rawurlencode("'"),$line); 
?>
echoes like this - test''s

can anybody explain why I cannot correctly replace the apostrophes from a form upload code?
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

maybe because you have the arguements the wrong way around....

Code: Select all

$line = str_replace(rawurlencode("'"),rawurlencode(" "),$line);
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

no...still echoes as
apostrophe''s .
Cheers anyway though.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Why use rawurlencode in the first place?
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

because I was using regular str_replace and my apostrophes were coming out as . . .
cat ' ' s
I read about str_replace on php manual and far down the page I found some advice about using rawurlencode for special characters...
I am prepared to admit I might have jumped on the wrong bandwagon, but what is the right one?
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

if its just form data then you dont need the rawurlencode, but you still had the arguements. try ...

Code: Select all

$line = str_replace("'"," ",$line);
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

I have mashed my code to look like this:

view.php

Code: Select all

<?php
<?
$data = file('./news.txt'); 
$data = array_reverse($data);
foreach($data as $element) {
    $ele = trim($ele);
$string = $pieces[3]; 
$pattern = array('!\[img\](.+?)\[/img\]!i', '!\[URL=(.*?)\](.*)\[/URL\]!i'); 
$replacement = array('<img src="\\1">', '<a class="whitearialsm"  href="http://\\1">\\2</a>'); 
}
foreach($data as $element) {
    $element = trim($element);
    $pieces = explode("|", $element);
{
    echo "<form class="form"><b>". $pieces[1] . "</b>" . $pieces[0]. "<br>" ; 
echo $pieces[2] .  "<br>" .  "<small>posted by<i> ".$pieces[3] ."</i></small></form>"  ; 
} 
}
?>
?>
and
addnews.php

Code: Select all

<?php
<?php 
$fp = fopen('./news.txt','a+'); 
if($HTTP_POST_VARS['submit']) 
         { 
                  $line = date("m.d.y")  . "|" . $HTTP_POST_VARS['name']; 
                  $line .= "|" . $HTTP_POST_VARS['news']. "|" . $HTTP_POST_VARS['poster']; 
                  $line = str_replace("\r\n","<br>",$line);
$line = str_replace("'"," ",$line);
                                 $line .= "\r\n";  
                 fwrite($fp, $line); 
            } 

?> 
?>
however apostrophes still print incorrectly?

aghhhhhhhhhh
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Why not just use stripslashes()?

Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

what are magic_quotes_gpc and magic_quotes_runtime set to on your system?
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

This might or might not be useful http://www.pinkgoblin.com/quotesarticle.php.
Post Reply