Jump to content


Photo

How to replace "-" to space?


  • Please log in to reply
6 replies to this topic

#1 urber

urber
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 06 August 2006 - 06:51 AM

Hi,

I got a problem here, how to replace - to space (empty).

I got a sentence for example:

Going to shopping - in the afternoon

I want to replace the - and the sentence will become:

Going to shopping in the afternoon


PLease kindly help thank you.






#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 06 August 2006 - 06:54 AM

example

<?php

$word=" my name is - redarrow and i like shopping":

$replace_word=str_replace("-"," ",$word);

echo $replace_word;

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 August 2006 - 06:54 AM

Use the function str_replace();
<?php
$str = 'Going to shopping - in the afternoon';
$new = str_replace('-',' ',$str);
echo $new;
?>

Ken

#4 urber

urber
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 06 August 2006 - 07:34 AM

Hi guys,

thanks for reply but it doesn't seem to work..

I got a script to draw out title from the mysql database and write to xml.

But when it met sentence with - it will change to ?

and than xml will give me this error:

line 96, column 41: title contains bad characters [help]

<title>Going to shopping ? in the afternoon</title>..

my code:



<?php


$select = "SELECT * FROM table1 ORDER BY id DESC LIMIT 18";
$query = mysql_query($select) or die(mysql_error());

$file = fopen("feed.xml", "w");

fwrite($file, "<?xml version='1.0'?>
<rss version='2.0'>
<channel>
<title>Whatever</title>
<link>http://www.domain.com</link>
<description>my desc</description>
<language>en-us</language>");

function chopSent($varb,$num) {
$dnum = intval($num);
if (strlen($varb) > $dnum) {
$nvarb = substr($varb, 0, $dnum);
$nvarb .= "..." ;
} elseif (strlen($varb) < $dnum) {
$nvarb = $varb;
}
return $nvarb;
}

while($array = mysql_fetch_array($query)){
extract($array);

$sentence2 = $content;
$sentence2 = str_replace("-"," ",$sentence2);
$sentence2 = stripslashes($sentence2);
$sentence2 = strip_tags($sentence2);
$sentence2 = chopSent($sentence2, 150);

$new = $maintitle; // Going to shopping - in the afternoon << this is the title
$new = str_replace("-"," ",$new);

// $content and $maintitle are drawn from my database both have this - problem when print to xml,
// tried using the code but still printing ? instead of blank.

fwrite($file, "<item>

<title>$new</title>
<link>http://www.domain.co...p?id=$id</link>
<description>$sentence2</description>

</item>");
}
//end of while loop

fwrite($file, "</channel></rss>");

fclose($file);




Xml print out results:

-<rss version="2.0">
−<channel>
<title>Whatever</title>
<link>http://www.domain.com</link>
<description>my desc</description>
<language>en-us</language>

<title>
Going to shopping ? in the afternoon <<< --- this ? cause error.
</title>

−<link>
http://www.domain.co...ex.php?id=69630
</link>

−<description>
Story description
</description>


Any helps?

Thanks

#5 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 06 August 2006 - 09:08 AM

The PHP manual states

The following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)


So best thing to do is
$replace_word=str_replace("-","",$word);


RTFM is an almost extinct art form, it should be subsidized.

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 06 August 2006 - 11:16 AM

Actually, you want to replace "- " with "" to avoid the double space that would otherwise result with " - " having just "-" replaced.
Legend has it that reading the manual never killed anyone.
My site

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 August 2006 - 01:25 PM

This question & answer stream shows how important it is to enter all pertanent information when the initial question is asked. If the OP had included the example that was provided after two answers were given, those answers would have been different.

The example given clearly shows that what the OP wants to replace is not a common dash ("-"), but most likely an emdash or endash saved from another document.

Take a look at http://en.wikipedia.org/wiki/Dash for an explanation of the codes used.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users