简体   繁体   中英

Print special characters like \s or \n as '\s' or '\n'

I have a string of text with whitespace and newlines. Sometimes tabs. eg

$text = 'Hello I am    chunk  of text
        A new line follows';

I'm then doing some matching with preg_match.

For example:

preg_match('/Hello I am\s*/',$a,$b);

But before I do that, I'd like to see exactly what kind of spacing is in this $text string so I would like to see what regex sees, otherwise I'm doing a fair bit of trial and error. My real $text string is actually far more complex and sometimes has tabs newlines and I have a suspicion some sort of separation I'm unaware of. So I'd like to see what regex sees, for example:

Hello\sI\am\tchunk\s\sof\stext\nA\snew\sline\sfollows

It doesn't translate the spaces and tabs and new lines but prints what they are. How is this done? I want to see what php sees when I through this $text string into a preg_match. Thanks

You can see newline characters (and maybe others) by using json_encode()

$string = 'something
            something';

echo json_encode($string);

// outputs something \nsomething

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM