Author Topic: [SOLVED] replacing characters in a string  (Read 355 times)

0 Members and 1 Guest are viewing this topic.

Offline nbaroneTopic starter

  • Enthusiast
  • Posts: 90
  • Gender: Male
    • View Profile
[SOLVED] replacing characters in a string
« on: July 02, 2009, 10:00:12 AM »
I have built a CMS to change content on a webpage, however, 90% of the content is givin to me in MS .docx format, and the people who write the content use lots of special characters.
Here is what I have to switch them over:
Code: [Select]
$content = $_REQUEST['pg_u_content'];
$content = ereg_replace("’","'",$content);
$content = ereg_replace("”","\"",$content);
$content = ereg_replace("“","\"",$content);
$content = ereg_replace("–","-",$content);
$content = ereg_replace("é","e",$content);

is there an easier way? It seems that their would be...
ehhh mannnn, pass the PHP.

Offline 947740

  • Devotee
  • Posts: 752
  • Gender: Male
  • Yeah.
    • View Profile
Re: replacing characters in a string
« Reply #1 on: July 02, 2009, 10:03:55 AM »
I'm experienced.  By my own standards.

Offline nbaroneTopic starter

  • Enthusiast
  • Posts: 90
  • Gender: Male
    • View Profile
Re: replacing characters in a string
« Reply #2 on: July 02, 2009, 10:10:26 AM »
http://us2.php.net/str_replace

Thanks, this is what I have now:
Code: [Select]
$content = $_REQUEST['pg_u_content'];
$c_find = array("’","”","“","–","é");
$c_replace = array("'","\"","\"","-","é");
$content = str_replace($c_find,$c_replace,$content);

However, is this the most efficient way to handle this?
ehhh mannnn, pass the PHP.

Offline 947740

  • Devotee
  • Posts: 752
  • Gender: Male
  • Yeah.
    • View Profile
Re: replacing characters in a string
« Reply #3 on: July 02, 2009, 10:16:04 AM »
You did not ask for a more efficient way, but I would imagine it would be once you get a lot of characters.  Having to run one function several times compared to running one function once...

As to your initial question, it would be easier (to me, at least) just to have arrays, because you just tack on characters to the end of each array, instead of creating a whole new line of code.
I'm experienced.  By my own standards.

Offline Mark Baker

  • Addict
  • Posts: 1,586
  • Gender: Male
    • View Profile
Re: replacing characters in a string
« Reply #4 on: July 02, 2009, 10:18:42 AM »
Or it might be better to use htmlentities() or htmlspecialchars()
9 out of 10 PHP problems can be resolved by setting
Code: (php) [Select]
error_reporting(E_ALL);
ini_set('display_errors', 1);
php -l <filename> will identify 9 out of the remaining 10 problems
Remember, the command line is your friend
Development Projects: PHPExcel and PHPPowerPoint

Offline nbaroneTopic starter

  • Enthusiast
  • Posts: 90
  • Gender: Male
    • View Profile
Re: replacing characters in a string
« Reply #5 on: July 02, 2009, 10:19:06 AM »
You did not ask for a more efficient way, but I would imagine it would be once you get a lot of characters.  Having to run one function several times compared to running one function once...

As to your initial question, it would be easier (to me, at least) just to have arrays, because you just tack on characters to the end of each array, instead of creating a whole new line of code.

Thank you, by "most efficient" way I meant was the something similar to htmlentities that converted special characters to HTML-readable characters. This will work fine, since this CMS will only be used periodically.

Thank you very much for the help.
ehhh mannnn, pass the PHP.

Offline nbaroneTopic starter

  • Enthusiast
  • Posts: 90
  • Gender: Male
    • View Profile
Re: replacing characters in a string
« Reply #6 on: July 02, 2009, 10:21:05 AM »
Or it might be better to use htmlentities() or htmlspecialchars()

Thanks, but neither of those do what I am trying to accomplish.
ehhh mannnn, pass the PHP.