检查wordpress帖子内容中是否有以.jpg,.png或.gif结尾的有效网址

时间:2013-12-11 04:57:43

标签: regex wordpress image validation url

我正在做一个前端表单,让用户提交一个有效但也是图像的网址,以.jpg,.png或.gif结尾。

这是我的表格:

<form action="" id="primaryPostForm" method="POST">

    <fieldset>
<p>Accepting GIF/JPG/PNG URL (Max size: 3MB)</p>
        <label for="postTitle"><?php _e('* POST TITLE:', 'framework') ?></label>

        <input type="text" name="postTitle" id="postTitle" class="required" value="<?php if ( isset( $_POST['postTitle'] ) ) echo $_POST['postTitle']; ?>" />
    </fieldset>

    <fieldset>
        <label for="postContent"><?php _e('* URL:', 'framework') ?></label>

        <textarea name="postContent" id="postContent" class="required" <?php if ( isset( $_POST['postContent'] ) ) { if ( function_exists( 'stripslashes' ) ) { echo stripslashes( $_POST['postContent'] ); } else { echo $_POST['postContent']; } } ?>></textarea>
    </fieldset>

<fieldset>
        <input type="hidden" name="submitted" id="submitted" value="true" /> 
        <button type="submit"><?php _e('SUBMIT', 'framework') ?></button>
    </fieldset>


</form>

这就是我获取INFO并创建帖子的方式:

if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) {

    $post_information = array(
        'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
        'post_content' => $_POST['postContent'],
        'post_type' => 'post',
        'post_status' => 'publish'
    );
$new_post = wp_insert_post( $post_information );

我想检查post_content中插入的字符是否与以.jpg / png / gif结尾的网址匹配,从那时起创建一个这样的条件标记:

如果$ _POST ['postContent'] ==在JPG / PNG / GIF中结束的网址{ 做某事}

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

以下是网址的基本匹配以及最后的jpg / png / gif选项:

\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]\.(?:jpg|png|gif)\b

这在我脑海中响起了警钟,但如果有人想在这种形式中注入某种代码,它可能会带来安全风险,他们可能会这样做。

希望您的代码可以区分图像和脚本。