I want to do treatments on two strings, and I must to know if there are spaces to stop my treatment and move on to the rest of characters
I tested that but it doesn't rule the problem :
for ($i = 0; $i < $lenght; $i++) {
if($text[$i] <> '' && $mask[$i] <> ''){
$nbrcrypted = $stringtonumber[$text[$i]] + $stringtonumber[$mask[$i]];
$resultat .= $numbertostring[$nbrcrypted];
}else{
$indice = false;
}
}
how can I achieve that, thank you in advance
if($text[$i] <> '' && $mask[$i] <> ''){ is useless, use if($text[$i] !== ' ' && $mask[$i] !== ' '){ for spaces
I don't get what you're actually trying to do but if you want to test your string for whitespaces:
if ( strpos( $yourString, ' ' ) !== false )
null; // string has whitespace(s)
Related
Is there any way to find and remove the url or domain from given string with parentheses ?
example: (like someurl.com) should be (like) and [like someurl.com] to become [like] ... also [like someurl.com/path/something.html] should be [like]
maybe someone can help me with a code to do this.
You need a Regular Expression (regx) here
$string_with_url = 'lorem (like GOoogle.COM someurl.com/path/something.html ) lipsum';
$string_without_url = preg_replace("/(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-zA-Z0-9]+([\-\.]{1}[a-zA-Z0-9]+)*\.[a-zA-Z]{2,5}(:[0-9]{1,5})?(\/.+[a-zA-Z\/]\s)?/", "", $string_with_url);
echo $string_without_url; // lorem (like ) lipsum
$str = "(like someurl.com)";
$index_dot = $index_space_before_dot = $index_after_dot = -1;
for($i=0; $i<strlen($str); $i++){
if($str[$i] == '.'){
$index_dot = $i;
}
if($str[$i] == ' ' && $index_dot == -1 && $index_space_before_dot == -1){
$index_space_before_dot = $i;
}elseif ($str[$i] == ' ' && $index_dot > -1){
$index_space_before_dot = $i;
}
}
$str = substr($str, 0, $index_space_before_dot);
echo $str . ')';
What's wrong with this code. I want to read the number of blank spaces without using any built in function, but it wont return or read the blank spaces:
$string = "can you look into this??";
$i = 0;
$breakPoints = 0;
while ($string[$i] != '' & $string[$i + 1] != '') {
if ($string[$i] == "" || empty($string[$i])) {
die("cdsd");
$breakposition = $string[$i];
$breakPoints++;
} else {
print_r($string[$i]);
}
$i++;
}
echo($breakPoints);
It's always going into the else part and never goes into the if statement. I even tried using isset() but that also didn't work. Where am I making a mistake?
Just loop while the string offset isset() and check if it equals a space. No need to do anything with $i+1:
$string = "can you look into this??";
$i = 0;
$breakPoints = 0;
while (isset($string[$i])) {
if ($string[$i] == " ") {
$breakposition = $string[$i];
$breakPoints++;
} else {
print_r($string[$i]);
}
$i++;
}
echo($breakPoints);
This outputs:
canyoulookintothis??4
Once you've got your code right, you will always run into an string index error and you will need the isset() built in function to check before performing operations.
In other words, the i for the index will eventually point beyond the last letter of the string, this will cause a PHP error. You can use isset() to check for it and break out of the loop. Example:
$string = "can you look into this??";
$i = 0;
$breakPoints = 0;
while (isset($string[$i])) {
if ($string[$i] == " ") {
$breakPoints++;
} else {
if($string[$i] != ''){
print_r($string[$i]);
}
}
$i++;
}
echo("<br />Number of spaces: ".$breakPoints
spaces is not empty, it will tack size.
so use this
$string = "can you look into this??";
$i = 0;
$breakPoints = 0;
while ($string[$i] != '' & $string[$i + 1] != '') {
if ($string[$i] == " ") {
echo " ";
$breakposition = $string[$i];
$breakPoints++;
} else {
print_r($string[$i]);
}
$i++;
}
echo($breakPoints);
DEMO
or try this code,
use preg_match_all.
$matches = " ";
$numSpaces = preg_match_all('/[ ]/', $string , $matches);
or Use this::
substr_count($string , ' ');
What is the difference of between the following examples?
FIRST EXAMPLE
if($x != '' or $y != '' or $z!=''or $c!=''){
echo "<h3>YOUR INPUT:</h3>";
echo $x.'<br>'.$y.'<br>'.$z.'<br>'.$c.'<br>';
}
SECOND EXAMPLE
if(!($x == '' or $y == '' or $z==''or $c=='')){
echo "<h3>YOUR INPUT:</h3>";
echo $x.'<br>'.$y.'<br>'.$z.'<br>'.$c.'<br>';
}
Please explain. I'm newbie in programming. I couldn't get it when someone post it in my question and I saw the code I thought it was the same as the title, but I tried it and I saw the difference. Help me to understand this.
$x != '' or $y != '' or $z!=''or $c!='' is true if any of the variables are not empty. If any of the variables are abc or are otherwise not '', the condition is true.
!($x == '' or $y == '' or $z==''or $c=='') is true only if all of the variables are not empty. Another more readable expression of those conditions is:
$x != '' and $y != '' and $z != '' and $c != ''
use this code is better you know what is it mean x,y,z & c is not empty give result. || is mean or.
if(!empty($x) || !empty($y) || !empty($z) || !empty($c)){
echo "<h3>YOUR INPUT:</h3>";
echo $x.'<br>'.$y.'<br>'.$z.'<br>'.$c.'<br>';
}
The first example would work if any of the four variables is not empty.
The second example would work only when none of the four variables is empty.
How can I format this code block so that every time this loop happens,
it moves each hyperlink element 20px from the left?
It's working at the moment but for the whole div not single items.
Example:
- LINK 1
-- LINK 2
--- LINK 3
Any help would be appreciated!
$linkArray = array();
$thisDir = '';
$baseDir = ($htmlRoot == '') ? '' : $htmlRoot;
for ($n=0; $n<count($dirArray); $n++) {
$thisDir .= $dirArray[$n].'/';
$thisIndex = MPBCDirIndex($htmlRoot.$thisDir);
$thisText = ($n == 0) ? $topLevelName : MPBCFixNames($dirArray[$n]);
$thisLink = ($thisIndex != '') ? '<span style="padding-left:20px;">'.$thisText.'</span>' : $thisText;
if ($thisLink != '') $linkArray[] = $thisLink;
}
$results = (count($linkArray) > 0) ? implode($separator, $linkArray) : '';
Well hmmm. You are already counting your iterations with the $n variable. SO:
EG.
for ($n=0; $n<count($dirArray); $n++) {
$pxvar = $n * 20;
$thisDir .= $dirArray[$n].'/';
$thisIndex = MPBCDirIndex($htmlRoot.$thisDir);
$thisText = ($n == 0) ? $topLevelName : MPBCFixNames($dirArray[$n]);
$thisLink = ($thisIndex != '') ? '<span style="padding-left:'.$pxvar.'px;">'.$thisText.'</span>' : $thisText;
if ($thisLink != '') $linkArray[] = $thisLink;
}
Note: the first iteration will have a padding of 0px. Not sure if thats how you want it?
I need a way of taking an equation given as a string and finding it's mathematical answer, the big caveat is that I can't use eval().
I know the equation will only ever contain numbers, the four mathematical operators (i.e. * / + -) and parentheses, it may or may not have spaces in the string. Here's a couple of examples.
4 * 4
4+6/3
(3 / 2)*(4+8)
(4+8) * 2
I'm guessing that it's going to have to be done with some kind of regex?
Math expressions aren't regular. They're context-free.
Your best bet is to parse them using well-known math parsing algorithms like the shunting yard algorithm. All you have to worry about is implementing the algorithm in PHP. You might even be able to find PHP implementations of it online.
Just in case anybody's interested here is the algorithm I came up with in PHP for producing Reverse Polish Notation
function convertToRPN($equation)
{
$equation = str_replace(' ', '', $equation);
$tokens = token_get_all('<?php ' . $equation);
$operators = array('*' => 1, '/' => 1, '+' => 2, '-' => 2);
$rpn = '';
$stack = array();
$size = count($tokens);
for($i = 1; $i < $size; $i++) {
if(is_array($tokens[$i])) {
$rpn .= $tokens[$i][1] . ' ';
} else {
if(empty($stack) || $tokens[$i] == '(') {
$stack[] = $tokens[$i];
} else {
if($tokens[$i] == ')') {
while(end($stack) != '(') {
$rpn .= array_pop($stack);
}
array_pop($stack);
} else {
while(!empty($stack) && end($stack) != '(' && $operators[$tokens[$i]] >= $operators[end($stack)]) {
$rpn .= array_pop($stack);
}
$stack[] = $tokens[$i];
}
}
}
}
while(!empty($stack)) {
$rpn .= array_pop($stack);
}
return $rpn;
}