1

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?

3 Answers 3

3

See TRIM()

UPDATE MY_TABLE SET my_field=TRIM(BOTH '/' FROM my_field);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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']);

1 Comment

No need for array. $row['Field'] = trim($row['Field'], '/'); should do the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.