一個特殊字符的位置我要確定特殊字符的串例如在位置:確定該字符串在PHP
E77eF/74/VA上6和9的位置(從1開始計數) 我們有「/」,所以我不得不改變他們的位置編號 - > E77eF VA
在MSSQL我可以使用PATINDEX,但我需要使用PHP這一點。 它應該適用於除了0-9a-zA-Z之外的所有東西
我在php.net上發現了strpos()
和strrpos()
,但我對我的工作並不好。 反正試圖做這樣的事情?
一個特殊字符的位置我要確定特殊字符的串例如在位置:確定該字符串在PHP
E77eF/74/VA上6和9的位置(從1開始計數) 我們有「/」,所以我不得不改變他們的位置編號 - > E77eF VA
在MSSQL我可以使用PATINDEX,但我需要使用PHP這一點。 它應該適用於除了0-9a-zA-Z之外的所有東西
我在php.net上發現了strpos()
和strrpos()
,但我對我的工作並不好。 反正試圖做這樣的事情?
<?php
$content = 'E77eF/74/VA';
//With this pattern you found everything except 0-9a-zA-Z
$pattern = "/[_a-z0-9-]/i";
$new_content = '';
for($i = 0; $i < strlen($content); $i++) {
//if you found the 'special character' then replace with the position
if(!preg_match($pattern, $content[$i])) {
$new_content .= $i + 1;
} else {
//if there is no 'special character' then use the character
$new_content .= $content[$i];
}
}
print_r($new_content);
?>
輸出:
E77eF6749VA
可能不是最有效的方式,但工作。
$string = 'E77eF/74/VA';
$array = str_split($string);
foreach($array as $key => $letter){
if($letter == '/'){
$new_string.= $key+1;
}
else{
$new_string.= $letter;
}
}
echo $new_string; // prints E77eF6749VA