/    Sign up×
Community /Pin to ProfileBookmark

Regular expression takes also newline character but not specified

Dear users,

I have a string containing various characters. From that string I want to remove some characters like tabs and quotes.
I use this php code:

“`
<?
$str = ‘RegExr was created by gskinner.com, and is proudly hosted by Media Temple.

Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & JavaScript flavors of RegEx are supported. Validate your expression with Tests mode.

The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community, and view patterns you create or favorite in My Patterns.

Explore results with the “Tools below. Replace & List output” custom results. Details’ lists capture groups. Explain des’cribes your expression in plain English.’;

echo preg_replace(‘/[cItvf”\’]/’, ”, $str);
?>
“`

I created a draft on regexr at this link: https://regexr.com/5buva

On that website, the newline character, not specified in regular expression, is left there. But in my php function, it is removed.

I don’t want to remove newline character from the string.

How can I fix?

Thanks

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 13.2020 — It might be easier to tell us exactly what you're trying to accomplish here -- and perhaps _why_, as well. Then we can help you decide if a regex approach is the best way, and if so, recommend the right search string. (I don't see why that pattern would include newlines (n), but I also don't know why I'd ever want to use that pattern in any case.)
Copy linkTweet thisAlerts:
@VITSUSASep 14.2020 — @redagr#1623239 I agree with NogDog, what you're trying to accomplish here? We can help you decide if a regex approach is the best way, and if so, recommend the right search string.
Copy linkTweet thisAlerts:
@redagrauthorSep 14.2020 — I want to remove single and double quotes, tab and other similar characters from a string to store in a mysql table.

This is what I'm doing.
Copy linkTweet thisAlerts:
@NogDogSep 14.2020 — > @redagr#1623265 I want to remove single and double quotes, tab and other similar characters from a string to store in a mysql table.

Again: why? MySQL character and text fields can handle "special characters" just fine. If Conan O'Brien and Catherine Zeta-Jones register for your site, are you going to store their last names as "OBrien" and "ZetaJones" (or "O Brien" and "Zeta Jones")?

Anyway, it's going to get messy. You probably want tabs to be converted to spaces, while things like quotes just get stripped out, so you may need to do two replacement passes on it (one converting certain characters to a space, and another to convert other characters to an empty string). This may be too aggressive, but...?
<i>
</i>$filtered_text = preg_replace('/t/', ' ', $text); // tabs -&gt; spaces
$filtered_text = preg_replace('/[^sw]/', '', $filtered_text); // remove non-white-space, non-word characters
Copy linkTweet thisAlerts:
@redagrauthorSep 14.2020 — Thanks for your reply.

I want to follow your advice, replacing all the tabs (cItvf, not only t) with a single space and stripping out only double qoutes, leaving the single quotes intact.

How can I do?

Thanks
Copy linkTweet thisAlerts:
@NogDogSep 14.2020 — Looks to me like the v is removing the newline. Maybe try:
<i>
</i>$filtered = preg_replace('/[cItfr]/', ' ', $text);
$filtered = str_replace('"', '', $filtered);
Copy linkTweet thisAlerts:
@redagrauthorSep 15.2020 — Thanks for reply, now I fixed the issue.
×

Success!

Help @redagr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...