0

I have a text like:

Word word word <a href=google.com>word word word</a> word word word

I need the positions of spaces, which are not enclosed by any html tag - positions of spaces underlined below:

Word_word_word_<a href=google.com>word word word</a>_word_word_word

What regex should I use (in preg_match())?

1

2 Answers 2

1

A rather straightforward approach:

$test = 'Word word word <a href=google.com>word word word</a> word word word';
$t = preg_replace('#\s+((?=.*<a)|(?!.*</a>))#', '_', $test);
var_dump($t); 
// string 'Word_word_word_<a href=google.com>word word word</a>_word_word_word'

Obviously, it won't work for the strings that may contain more than one embedded href; for these cases I would suggest another approach: 1) split the string by <a href...>...</a> parts, 2) replace all the whitespace in non-linked parts; 3) reconstruct the string.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, this is what I was looking for. There will not be embedded tags in my text.
I have one more question: how to change it to not match spaces in IMG tags, too? EDIT: ok, i have it: #\s+((?=.*<a)|(?=.*<img)|(?!.*>))# :)
@Siorus If these tags are strictly exclusive in the processed strings, and your HTML is well-formed, replace <a and </a> with <img and /> correspondingly in the regex given. Otherwise, you'd better use an HTML parser.
0

Use the str_replace function:

$string = "Sample Sample Sample Sample Sample";
$name = str_replace(' ', '_', $string );
echo $name;

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.