开发者

Space Characters Getting Converted to Unknown Character

I've got a problem with a module that I am writing. What I have is an array of names, which has a particular format and can contain characters as well as spaces. The name can be of two formats:

$name[0] = "--> Psychic Barrtier";
$name[1] ="    Initial Presence";

In my code, I take each line in the array, and use a preg_match statement to see if it matches these two patterns. So, I am basically looking for a line that starts with two dashes and '>' this, followed by a space. Or I am looking for a line that starts with 4 spaces. This is my preg_match statement:

while (preg_match('/^(--> |--> |--> | {4}|    |Â+)(.+)$/', $name[$i], $capturedname))

The problem is, both types of names are passing the preg_match statement, but when I encounter a name that starts with just the 4 spaces, the captured data in the variable $capturedname doesn't match up. Basically, what I'm capturing in $caputredname[0] is the whole thing, then $capturedname[1] would be just the spaces, and $capturedname[2] would be just the name. This is what I get instead:

$capturedname[0]=" Evil Presence" $capturedname[1] = "�" $capturedname[2] = "� Evil Presence"

Array 1 plus 2 should equal Array 0, but that开发者_运维技巧's not the case here. The spaces are getting converted to some diamond with a question mark character and $capturedname[2] has the spaces again, it just doesn't add up. Any sort of help would be greatly appreciated. It's been bugging me for 3 days now. Thanks in advance.


I don't really think it's a conversion issue. Are you sure you don't have any formatting/other exotic characters in your input strings?

I have just done a quick test based on your array and pattern (though I'm not sure what that 'Â+' is meant to do there? Unless you expect such exotic strings...) and it worked pretty well for me:

$name = array(
  "--> Psychic Barrtier",
  "    Initial Presence",
);
foreach ($name as $key => $value) {
  preg_match('/^(--> |--> |--> | {4}|    |Â+)(.+)$/', $value, $capturedname);
  print_r($capturedname);
}

which gave me following result:

Array
(
    [0] => --> Psychic Barrtier
    [1] => --> 
    [2] => Psychic Barrtier
)
Array
(
    [0] =>     Initial Presence
    [1] =>     
    [2] => Initial Presence
)

So I think first I would check whether your input strings are really what you expect them to be...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜