For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > October 2007 > adding space between words preg_replace









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author adding space between words preg_replace
joanna

2007-10-29, 7:02 pm

Hi, I'm using preg_replace to strip out a bunch of unwanted stuff from
the database. However the results seem to bunch up. Here's what I'm
doing

$old = htmlspecialchars($row['name']);

$new = preg_replace("/[^a-zA-Zs]/", "", $old);

the results of this are

TomSmith

instead of

Tom Smith

What do I need to add to the expression to get the desired space
between words?

Jerry Stuckle

2007-10-29, 7:02 pm

joanna wrote:
> Hi, I'm using preg_replace to strip out a bunch of unwanted stuff from
> the database. However the results seem to bunch up. Here's what I'm
> doing
>
> $old = htmlspecialchars($row['name']);
>
> $new = preg_replace("/[^a-zA-Zs]/", "", $old);
>
> the results of this are
>
> TomSmith
>
> instead of
>
> Tom Smith
>
> What do I need to add to the expression to get the desired space
> between words?
>
>



Untried, but maybe something like:

$new = preg_replace("/[^a-zA-Z\s]/", "", $old);

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Rik Wasmus

2007-10-29, 7:02 pm

On Mon, 29 Oct 2007 21:42:22 +0100, joanna <me@barrelofcrafts.com> wrote:

> Hi, I'm using preg_replace to strip out a bunch of unwanted stuff from
> the database. However the results seem to bunch up. Here's what I'm
> doing
>
> $old = htmlspecialchars($row['name']);
>
> $new = preg_replace("/[^a-zA-Zs]/", "", $old);
>
> the results of this are
>
> TomSmith
>
> instead of
>
> Tom Smith
>
> What do I need to add to the expression to get the desired space
> between words?


$new = preg_replace("/[^\w\s]/", "", $old);

Then again, why do a htmlspecialchars() if you're going to maim them
anyway? There's '&',';',0-9, etc... 'André Tös' might object to being
called 'Andreacute Toumls'. Also, try to store information as it is in a
database, not the presentation. While it might seem a good idea to use
htmlspecialchars() once here, it will be a pain when you're going to
search the database, or output data in other formats like a downloadable
report (pdf, txt, whatever).

What is it exactly that you don't want there, and why?
--
Rik Wasmus
joanna

2007-10-29, 7:02 pm

On Oct 29, 5:31 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Mon, 29 Oct 2007 21:42:22 +0100, joanna <m...@barrelofcrafts.com> wrot=

e:
>
>
>
>
>
>
>
>
> $new =3D preg_replace("/[^\w\s]/", "", $old);
>
> Then again, why do a htmlspecialchars() if you're going to maim them
> anyway? There's '&',';',0-9, etc... 'Andr=E9 T=F6s' might object to being
> called 'Andreacute Toumls'. Also, try to store information as it is in a
> database, not the presentation. While it might seem a good idea to use
> htmlspecialchars() once here, it will be a pain when you're going to
> search the database, or output data in other formats like a downloadable
> report (pdf, txt, whatever).
>
> What is it exactly that you don't want there, and why?
> --
> Rik Wasmus


That worked and yes I removed the htmlspecialchars as it is in fact
redundant.

Thank You

joanna

2007-10-29, 10:07 pm

On Oct 29, 5:31 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Mon, 29 Oct 2007 21:42:22 +0100, joanna <m...@barrelofcrafts.com> wrot=

e:
>
>
>
>
>
>
>
>
> $new =3D preg_replace("/[^\w\s]/", "", $old);
>
> Then again, why do a htmlspecialchars() if you're going to maim them
> anyway? There's '&',';',0-9, etc... 'Andr=E9 T=F6s' might object to being
> called 'Andreacute Toumls'. Also, try to store information as it is in a
> database, not the presentation. While it might seem a good idea to use
> htmlspecialchars() once here, it will be a pain when you're going to
> search the database, or output data in other formats like a downloadable
> report (pdf, txt, whatever).
>
> What is it exactly that you don't want there, and why?
> --
> Rik Wasmus


That worked and yes I removed the htmlspecialchars as it is in fact
redundant.

Thank You

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com