dongraa1986
2013-11-14 22:10
浏览 164
已采纳

如果没有反斜杠,则仅在前向斜杠上使用PHP分割字符串

I am making a parser for my SlashCategory, which requires me to split (or tokenize) each string based on the / character. I am using PHP's explode() function, which works well. For example, it takes the following:

Book/Title/Lord Of The Flies/Author/William Golding

And creates an array with:

[0] Book
[1] Title
[2] Lord Of The Flies
[3] Author
[4] William Golding

However, I have a problem. I do not want explode() to break up the string if the forward slash is preceded by a backslash. For example:

Url/Google/http:\/\/www.google.com

I want to have an array containing:

[0] Url
[1] Google
[2] http://www.google.com

Not:

[0] Url
[1] Google
[2] http:
[3] 
[4] www.google.com

How can I do this?

图片转代码服务由CSDN问答提供 功能建议

我正在为 SlashCategory ,它要求我根据 / 字符拆分(或标记化)每个字符串。 我使用PHP的 explode()函数,效果很好。 例如,它需要以下内容:

书籍/标题/苍蝇之王/作者/ William Golding

并创建一个数组:

  [0] Book 
 [1] Title 
 [2] Lord of The Flies 
 [3]作者
 [4]  William Golding 
   
 
 

但是,我遇到了问题。 如果正斜杠前面有反斜杠,我不希望 explode()分解字符串。 例如:

Url / Google / http:\ / \ / www.google.com

我想要 有一个数组包含:

  [0] Url 
 [1] Google 
 [2] http://www.google.com 
  <  / pre> 
 
 

不:

  [0] Url 
 [1] Google 
 [2] http:
 [3] 
  [4] www.google.com 
   
 
 

我该怎么做?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题