Matt
Matt

Reputation: 3876

MySQL string replace

Hey, what's the most effective way to remove beginning and ending slashes from all rows in a particular column using MySQL?

Before:

/hello/world/
foo/bar/
/something/else
/one/more/*

After:

hello/world
foo/bar
something/else
one/more/*

...or maybe this should be done in PHP instead?

Upvotes: 1

Views: 1808

Answers (3)

Jrgns
Jrgns

Reputation: 25207

Your PHP option: (I'm assuming the fetched row is in $row)

$row['Field'] = explode('/', $row['Field']);
//Remove the empty elements
$row['Field'] = array_filter($row['Field']);
$row['Field'] = implode('/', $row['Field']);

Upvotes: 1

raspi
raspi

Reputation: 6132

See TRIM()

UPDATE MY_TABLE SET my_field=TRIM(BOTH '/' FROM my_field);

Upvotes: 3

Andrew Hare
Andrew Hare

Reputation: 351758

You could definitelyt make this work using the MySQL string functions but I think this would be best handled outside of the database using PHP or whatever programming language of your choice.

Upvotes: 1

Related Questions