dqhbuwrwq692118284 2010-12-19 18:41
浏览 297

测试字符串中的反斜杠

So I'm writing a PHP script that will read in a CSS file, then put the comments and actual CSS in separate arrays. The script will then build a page with the CSS and comments all nicely formatted.

The basic logic for the script is this:

  • Read in a new line
  • If it starts with a forward slash or ends with an opening bracket, set a bool for CSS or comments to true
  • Add that line to the appropriate element in the appropriate array
  • If the last character is a backslash (end of a comment) or the first character is a closing bracket (end of a CSS tag), set necessary bool to false
  • Rinse, repeat

If someone sees an error in that, feel free to point it out, but I think it should do what I want.

The tricky part is the last if statement, checking if the last character is a backslash. Right now I have:

if ($line{(strlen($line) - 3)} == "\\") {do stuff}

where $line is the last line read in from the file. Not entirely sure why I have to go back 3 characters, but I'm guessing it's because there's a newline at the end of each string when reading it in from a file. However, this if statement is never true, even though there are definitely lines which end with slashes. This

echo "<br />str - 3: " . $line{(strlen($line)-3)};

even returns a backslash, yet the if statement is never trigged.

  • 写回答

2条回答 默认 最新

  • duanlian1978 2010-12-19 18:53
    关注

    That would be because $line{(strlen($line) - 3)} in your if statement is returning one backslash, while the if statement is looking for two. Try using

    substr($line, -2)
    

    instead. (You might have to change it to -3. The reason for this is because the newline character might be included at the end of the string.)

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程