Tim
Tim

Reputation: 7056

find white space between characters php

How can I trim white space between two specific characters in the same string? i.e. I just want to close the space between the first tag there and the start of the next word...

I want to turn this:

<code>

                                testing testing 123
test

into this:

<code>testing testing 123

test

The <code> will always be there, so perhaps I could use that as some sort of anchor point?

Thanks

Upvotes: 2

Views: 998

Answers (3)

xpapad
xpapad

Reputation: 4456

Assuming <code> always exists at the beginning of your string, you can use:

$str = preg_replace( '/^<code>\s+/', '<code>', $str )

The regular expression above will match a <code> tag at the beginning of a line (the ^ indicator) and remove all whitespace characters following it (the \s+) pattern.

Upvotes: 1

tim
tim

Reputation: 10176

Use RegExp, something like this:

$trimmed = preg_replace('/(<[^>]*>)\s*/', '\1', $input_string)

This will work on ANY html-tag ;)

Upvotes: 0

akira
akira

Reputation: 6117

$trimmed = trim(substr($in, 6));

that will remove the whitespace after <code> up to the first non-whitespace.

Upvotes: 2

Related Questions