简体   繁体   English

简单的PHP正则表达式问题

[英]Simple PHP Regex question

I'd like to validate a field in a form to make sure it contains the proper formatting for a URL linking to a Vimeo video. 我想验证表单中的字段,以确保它包含链接到Vimeo视频的URL的正确格式。 Below is what I have in Javascript, but I need to convert this over to PHP (not my forte) 下面是我在Javascript中的内容,但我需要将其转换为PHP(不是我的强项)

Basically, I need to check the field and if it is incorrectly formatted, I need to store an error message as a variable.. if it is correct, i store the variable empty. 基本上,我需要检查字段,如果格式不正确,我需要将错误消息存储为变量..如果它是正确的,我将变量存储为空。

                // Parse the URL
            var PreviewID = jQuery("#customfields-tf-1-tf").val().match(/http:\/\/(www.vimeo|vimeo)\.com(\/|\/clip:)(\d+)(.*?)/);
            if ( !PreviewID ) {
                jQuery("#cleaner").html('<div id="vvqvideopreview"><?php echo $this->js_escape( __("Unable to parse preview URL. Please make sure it's the <strong>full</strong> URL and a valid one at that.", 'vipers-video-quicktags') ); ?></div>');
                return;
            }

The traditional vimeo url looks like this: http://www.vimeo.com/10793773 传统的vimeo网址如下所示: http//www.vimeo.com/10793773

Thanks! 谢谢!

if (0 === preg_match('/^http:\/\/(www\.)?vimeo\.com\/(clip\:)?(\d+).*$/', $value))
{
    $error = 'Unable to parse preview URL';
}

Update, in reply to your comment: 更新,回复您的评论:

if (0 === preg_match('/^http:\/\/(www\.)?vimeo\.com\/(clip\:)?(\d+).*$/', $value, $match))
{
    $error = 'the error';
}
else
{
    $vimeoID = $match[3];
}

Try this for https / http url 试试https / http url

if (preg_match('/^(http|https):\/\/(www\.)?vimeo\.com\/(clip\:)?(\d+).*$/', $vimeo_url, $vimeo_id)){
    $vimeoid = $vimeo_id[4];
}else{
   // error message... 
}

Just parse your $_REQUEST with preg_match like. 只需用preg_match解析你的$ _REQUEST即可。

$vimeo_array = array();
$vimeo_link = $_REQUEST("form_input_name");
if(preg_match(/http:\/\/(www.vimeo|vimeo)\.com(\/|\/clip:)(\d+)(.*?)/, $vimeo_array, $arr))
{
  $vimeo_code = $vimeo_array[3];
} else {
  $error = "Not a valid link";
}

To get at the Vimeo ID number, you could do something like the following: 要获得Vimeo ID号,您可以执行以下操作:

$link = 'http://vimeo.com/10638288';
if (preg_match('~^http://(?:www\.)?vimeo\.com/(?:clip:)?(\d+)~', $link, $match)) {
    $vimeo_id = $match[1];
} else {
    // Show user an error, perhaps
}

I also slightly altered the regular expression, to save excessive backslash escape characters. 我还略微修改了正则表达式,以节省过多的反斜杠转义字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM