I didn't understand Regex on PHP. [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to clean all special characters from the string with regex code, so far I have did this code
$data = preg_replace('/[^a-zA-Z0-9]/s', '', $nickname);
But this code cleans everything including spaces, I want to allow space and - sign on the string, so what should I exactly add in there? I am sorry if it is an easy question but my English is not really good and I couldn't find any resources in my main language. Could anyone help me with that?

$data = preg_replace('/[^a-zA-Z0-9 -]/s', '', $nickname);
[^a-zA-Z0-9 .-]
Debuggex Demo

Related

Regex that finds JSON string inside another string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I would like to extract a JSON string within another string.
Currently I am getting the full string using file_get_contents and running the following pattern on the string: https://regex101.com/r/5jAucO/1
which pretty much gives multiple matches.
I would like to extract the JSON string that is saved in window._sharedData but haven't been able to achieve that. Does someone have any idea how I could do that?
Why not include _sharedData in the regex like?
_sharedData\s*=\s*\{.+\}
or with lookbehind:
(?<=_sharedData\s=)\s*\{.+\}
or take the json from a capturing group:
_sharedData\s*=\s*(\{.+\})
One concern with the lookbehind is if they add an additional whitespace character between _sharedData and = it won't match.
This only works well since there are no linebreaks in the JSON.

How can find "<< >>" words from string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have tried some regex pattern but still can not find some solution for this.
I want get << >> words from following string.
$string = "Yes! Bariatric surgery is covered under your plan. Click <<COMPANY>> here to contact a local surgeon and ask them to confirm your insurance benefits for free. See our <<INSURANCE>> page for coverage criteria or scroll down this page to learn more.";
$dash = substr($string,strpos($string,"<<"),strpos($string,">>"));
If any one knows please help me.
Thanks.
This regular expression will match the words between << and >>
\<{2}([^\>]+)\>{2}
https://regex101.com/r/eX4xI4/1
preg_match_all("/\<{2}([^\>]+)\>{2}/", $string, $matches);
print_r( $matches );
https://eval.in/602428

php remove empty single comment [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have code like this
function get_id(){
return 1;
//
}
I want to remove all // inside the source code, but only those stand by it own, on a newline just like the example here. Nothing much nothing more. How can I do it?
Search using this regex:
^\s*\/\/\s*$
and replace by empty string.
If using PHP you can use \h (horizontal space) instead of \s:
$code = preg_replace('~^\h*//\h*$~m', '', $code);

Strip HTML tags AND double quotes [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to clean my entries that look like
“If you think adventure is dangerous, try routine; it is lethal.”
<p>Life isn't about finding yourself it's about creating yourself. </p>
I need to remove HTML tags and double quotes and single quotes if they are in the beginning or the end of the string.
There was a function, but I don't remember what it was or something like a class...
You can remove everything with ^[“"'‘]|[”"'’]$|^<[^>]*?>|<[^>]*?>$
Here is a demo.

How to split a string between two delimiters in php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I would like to split a string from a whole string which has two delimiters. For example
String1= ABC*WELCOME*CDF
I should get out put as WELCOME.
Note: Delimiters can be any position of the string(not static) but I need only string between those two delimiters
Thanks in advance.
$String1 = 'ABC*WELCOME*CDF';
list( ,$myString, ) = explode('*',$String1);
echo $myString;

Categories