AS3 BBcode 还是替换技术?

发布于 2024-10-10 00:58:24 字数 461 浏览 0 评论 0原文

我正在用 AS3/PHP(数据库调用)制作一个网站,我想在主页上加载来自 mySQL 数据库的新闻。使用 AMFPHP 获取纯文本没有问题。但我正在寻找不同的东西。我知道如何在 AS3 中加载图像/YouTube 视频,所以我想用它来增加新闻的趣味性。有什么方法可以输入类似的内容,

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
[video]AAAAAAAAAAA[/video]
Donec commodo condimentum enim, vitae consectetur felis pharetra a.

在我的文本之间创建 YouTube API 类的实例并创建两个 TextFields? AS3 没有 BBcode 库,所以我的第二个猜测是......正则表达式。不过,我似乎无法理解这一点,有人想尝试同样的事情并成功吗?

提前致谢。

I'm making a website in AS3/PHP (databasecalls) and I want to load news on the homepage, fed from a mySQL database. No problem to get plain text with AMFPHP. But I'm looking for something different. I know how to load images/YouTube videos within AS3, so I want to spice up the news a little with that. Is there any way I can input something like

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
[video]AAAAAAAAAAA[/video]
Donec commodo condimentum enim, vitae consectetur felis pharetra a.

it would make an instance of my YouTube API class right between my text and make two TextFields? There's no BBcode library for AS3, so my second guess was ... regex. Can't seem to wrap my head around that though, anyone ever wanted to try the same thing and succeeded?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

开始看清了 2024-10-17 00:58:24

这对于正则表达式来说是一个完美的工作。

var myPattern:RegExp = /\[video\]([a-zA-Z0-9_-]+)\[\/video\]/g;  

括号[]和斜杠/是保留字符,因此需要用反斜杠转义。 [a-zA-Z0-9_-] 是有效 YouTube ID 中可以包含的字符范围。

要使用正则表达式,您可以这样进行:

var str:String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n[video]AAAAAAAAAAA[/video]\nDonec commodo condimentum enim, vitae consectetur felis pharetra a.";

var result:Object = myPattern.exec(str);
while (result != null) {
    trace( "id is: " + result[0] + " at index: " + result.index);
    result = myPattern.exec(str);
}

您还可以通过将分配作为 while 条件来将相同的代码压缩为更少的行:

var result:Object;
while (result == myPattern.exec(str)) {
    trace( "id is: " + result[0] + " at index: " + result.index);
}

这在功能上是等效的,但可能有点难以掌握。

现在您有了索引和 ID,剩下的就是拆分文本并插入视频!

This is a perfect job for a regular expression.

var myPattern:RegExp = /\[video\]([a-zA-Z0-9_-]+)\[\/video\]/g;  

The brackets [ ] and the slash / are reserved characters, so they need to be escaped with a backslash. [a-zA-Z0-9_-] is the range of characters that can be in a valid youtube id.

To use the regex you go like this:

var str:String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n[video]AAAAAAAAAAA[/video]\nDonec commodo condimentum enim, vitae consectetur felis pharetra a.";

var result:Object = myPattern.exec(str);
while (result != null) {
    trace( "id is: " + result[0] + " at index: " + result.index);
    result = myPattern.exec(str);
}

You can also compress the same code into fewer lines by putting the assingment as the while condition:

var result:Object;
while (result == myPattern.exec(str)) {
    trace( "id is: " + result[0] + " at index: " + result.index);
}

This is functionally equivalent, but can be a bit harder to grasp.

Now you have the index and the id, all that remains is to split up the text and insert the video!

止于盛夏 2024-10-17 00:58:24

您应该能够使其与 String split() 方法一起使用。定义分隔符并使用 split() 方法返回子字符串数组。

这是一个基本示例,我相信您可以对其进行优化......

 private var result:String = "Lorem ipsum dolor sit amet, consectetur adipiscing 
 elit [--video--]myexample.com/video_url[--video--] Donec commodo condimentum enim, vitae 
 consectetur felis pharetra a."

 private var separator:String = "[--video--]";

 //Assuming that you're only using one video...
 private function parseString(value:String ):void
 {
      var texts:Array = value.split( separator );

      if( texts.length >= 2 ) //there's a video
          initTextWithVideo( texts );
      else // it's only text
          initText( result );
  } 

  private function initTextWithVideo( value:Array ):void
  {
       var text1:String = value[0];
       var videoURL:String = value[1];
       var text2:String = value[2];
       //etc...
  }

You should be able to get this to work with the String split() method. Define a separator and use the split() method to return an Array of substrings.

Here's a basic example which I'm sure you can optimize...

 private var result:String = "Lorem ipsum dolor sit amet, consectetur adipiscing 
 elit [--video--]myexample.com/video_url[--video--] Donec commodo condimentum enim, vitae 
 consectetur felis pharetra a."

 private var separator:String = "[--video--]";

 //Assuming that you're only using one video...
 private function parseString(value:String ):void
 {
      var texts:Array = value.split( separator );

      if( texts.length >= 2 ) //there's a video
          initTextWithVideo( texts );
      else // it's only text
          initText( result );
  } 

  private function initTextWithVideo( value:Array ):void
  {
       var text1:String = value[0];
       var videoURL:String = value[1];
       var text2:String = value[2];
       //etc...
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文