0

I'm having trouble figuring out how to remove spaces, quote marks and periods in the first instance of $member['team_name'] (the one in the rel attribute) using str_replace.

foreach($members as $member) {
    echo '<div class="teamname" rel="' . $member['team_name'] . '">' . $member['team_name'] . '</div>';
}

2 Answers 2

1

str_replace should do it fine -

echo '<div class="teamname" rel="' . str_replace(' ', '', $member['team_name']) . '">' . $member['team_name'] . '</div>';

That will work for spaces, you could use preg_replace to remove a general whitespace pattern.

EDIT (having seen your edit!):

The preg_replace pattern you're probably after would be something like

echo '<div class="teamname" rel="' . preg_replace('/[^a-z0-9]+/i', '', $member['team_name']) . '">' . $member['team_name'] . '</div>';`

This will replace any non-alphanumeric character. If you really only want spaces, quote marks and periods, you can go back to a str_replace with an array as the search argument - it will be faster.

str_replace(array(' ','"',"'",'.'), '', $member['team_name'])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much. I didn't realize that I could just add the preg_replace inline. I was trying to figure out how to create another string.
0

You could use preg_replace() something like this:

preg_replace("/(?:\s|\t|\'|\.|\"')+/", "", $member['team_name'], -1)

That will remove all spaces, tabs, dots and either quote mark

Comments

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.