Jump to content

Search For Sole Character.


Err

Recommended Posts

Hello. Long time since I've posted here. But I'll get right to my question.I'm working on a PHP script where I'm trying to find the dash (-) character and if it exists by itself in a string. It doesn't matter how many dashes are in the string, but only if a dash exists just by itself (------------) with no other characters (0-9, a-z, A-Z). I'm NOT trying to replace it, remove it, or edit it out some how, I only want to check if it exists alone. Can someone please point me in the right direction? I seem lost with this one.I've tried:

foreach (count_chars($string, 1) as $i => $val) {  if (chr($i) == "-") {	// some code  }}

And I've also tried the str_split function. Perhaps I'm not tackling this the right way. Any help would be greatly appericated.

Link to comment
Share on other sites

If you only want to figure out if it exists in the string, use strpos:if (strpos($string, '-') !== false) { echo 'dash exists'; }If you're looking for something else, give some examples of strings you do and do not want to find.edit: maybe you're trying to figure out if a string only contains that character and nothing else. You could use a regex, this would also work:

$invalid = false;for ($i = 0; $i < strlen($string); $i++){  if ($string[$i] != '-')  {	$invalid = true;	break;  }}if ($invalid)  echo 'invalid characters';

Link to comment
Share on other sites

If you only want to figure out if it exists in the string, use strpos:if (strpos($string, '-') !== false) { echo 'dash exists'; }If you're looking for something else, give some examples of strings you do and do not want to find.edit: maybe you're trying to figure out if a string only contains that character and nothing else. You could use a regex, this would also work:
$invalid = false;for ($i = 0; $i < strlen($string); $i++){  if ($string[$i] != '-')  {	$invalid = true;	break;  }}if ($invalid)  echo 'invalid characters';

Thanks. The code you gave only checks if a dash exists in the string with alphanumeric characters. I want to check if dashs exists without alphanumeric characters. I only want to check if the string contains dashes and nothing else.Something like: "------------"I don't want to check for strings like this: "cat-dog-pig-frog"
Link to comment
Share on other sites

There's already an iterative solution for what you want, but just for completeness, example regex for that:

<?php$yes = '-------';$no  = '------0';echo (preg_match('#^-+$#',$yes)) ? 'match' : 'not match';// Outputs: matchecho (preg_match('#^-+$#',$no)) ? 'match' : 'not match';// Outputs: not match?>

That matches any input string containing one or more "-" characters (if you also want the empty string then incorporate "?" in the regex).

Link to comment
Share on other sites

Just for completeness, example regex for that:
<?php$yes = '-------';$no  = '------0';echo (preg_match('#^-+$#',$yes)) ? 'match' : 'not match';// Outputs: matchecho (preg_match('#^-+$#',$no)) ? 'match' : 'not match';// Outputs: not match?>

That matches any input string containing one or more "-" characters (if you also want the empty string then incorporate "?" in the regex).

Thanks for your help. I figured it out. But this thread certainly gave me some ideas.Here is the code to check if ONLY dashes exist in a string:
$array1 = str_split($string);for ($i = 0; $i < count($array1); $i++) {  if ($array1[$i] == "-") {	$dash++;	if ($dash == count($array1)) {	  echo "Only dashes exist in this string.";	  break;	}  }}

Link to comment
Share on other sites

/facepalmThis code works on my local computer. But when I put everything online I have trouble with it. The page loads fine, but when I do a search on it, it reloads with a blank page. Source code is telling me that it's only loading the header and stops loading at the <body> tag. I know it's this code because when I comment it out, it loads the page completely. Anyone have any idea what's going on? I've tried to edit my code and make it more compliant, but whatever I try it's not working.Edit: after running validation on it I found this error:Fatal error: Call to undefined function: str_split() in [path] on line 67Does this mean I don't have that function? On line 67 I have this code:$txt_array1 = str_split($txt_search);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...