PHP "Kill" all characters from string except A-Z and ";" [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
$string='A;B;C;1;2-;D'
how can i remove all characters from this the string above but keep the Letters and the ";"

Try this simple regex:
preg_replace('/[^A-Z;]+/', '', $string);
or
preg_replace('/[^A-Z;]+/i', '', $string);
If you need case insensitive.

You can use:
$repl = preg_replace('/[^a-z;]+/i', '', $str);
^a-z; inside square brackets (character class) means match anything but English letters a-z (range) OR a semi-colon ;
/i is for ignore case to avoid matching uppercase/lowercase english letters.

try this
preg_match('/[^a-z;]/i', $string);

Related

Remove string after dash IF end string matches given string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
What I'm trying to do is remove a string after the dash if the string after the dash matches what is appended.
For example it'd be like: if endofstring is equal to givenstring, remove dash and everything after, else keep the dash
Thanks for any help!
$string='Hello this is the-end';
$remove='end';
$i=strrpos($string, $remove);
if ($i===strlen($string)-strlen($remove)) $string=substr($string, 0, $i-1);
var_dump($string);

how i can use preg_match for check arabic and english and number and " - "letters? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
i want to check following by function preg_match:
1)Arabic letters
2)English letters
3)numbers
4)spaces, dashes ( - ) and single quotes ( ' )
i use php language
i tried
preg_match("~^[a-z\-'\s]{1,60}$~i", $nam)
You can use this character class : \p{Arabic}
Example:
preg_match("~^[a-z\-'\s\p{Arabic}]{1,60}$~iu", $nam)
strings are treated as utf8 with the \u modifier

Regular expression needed for "<!-- A-Za-z0-9 -->" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Need help with regular expression for <!-- A-Za-z0-9 --> where all the uppercase, smallcase and digits are allowed. Need a regular expression to find this chunk including the comment tags (<!-- -->).
[<!\-\-A-Za-z0-9\-\->]
This is a character class and the order doesn't matter in a character class. What will work however:
<!--\s*[A-Za-z0-9]+\s*-->
If you now want to include spaces inside the comment, you can use:
<!--\s*[A-Za-z0-9 ]+\s*-->
I used the \s* in case there are several white spaces between the <!-- and --> and the part to capture.
Use preg_match_all and if you want to make the regex shorter, you can use the i flag. something like this:
preg_match_all('~<!--\s*[a-z0-9]+\s*-->~i', $page, $matches);
This is all I can think of according to your question
#<!--([A-Za-z0-9]*)-->#

Merge hyphenated word from string in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Suppose a string contains "includ- ing" or "joi- ing". How would you search for and join hyphenated words in a string?
You could use a regular expression with preg_replace():
preg_replace('/-\s+/', '', $string);
This looks for a - followed by one or more whitespace characters and replaces them with the empty string.
// -- Get an array of words splitted with '- '
$splitted = explode('- ', $word);
// -- Rejoined words:
$joined = implode('', $splitted );

Replace HTML entities with regular expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like a regular expression in PHP to find all HTML entities such as "<br /> <br /> ..etc. " in order to remove them from a long string.
This one removes alpha, decimal and hex HTML entities:
$text = preg_replace('/&(?:[a-z\d]+|#\d+|#x[a-f\d]+);/i', '', $text);
Try using this regex... the [^\s]* one was capturing a whole lot other nonsense...
$text = preg_replace("/&(?:[a-z0-9]{2,8}|#[0-9]{2,3}+);/i", '', $text);
Try using strip_tags function, regex is not necessary here

Categories