2013-12-11 25 views
0

我正在做一個前端表單,讓用戶提交一個有效但也是圖片的網址,以.jpg,.png或.gif。檢查是否有效的URL以.jpg,.png或.gif結尾於wordpress文章內容

這是我的形式:

<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> 

這是我得到的信息並創建的帖子:

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字符匹配的URL以.jpg/png/gif結尾,並從這一點創建一個像這樣的條件標籤:

if $ _POST ['postContent'] == URL以JPG/PNG/GIF結尾{ do somet hing}

我該如何做到這一點?

+0

你有什麼試過?有很多例子來驗證URL(正則表達式),並且比較字符串結尾非常簡單。 –

回答

0

下面是一個URL基本匹配您的JPG/PNG/GIF末選項一起:

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

這在我的腦海裏響起了警鐘,雖然,它可能是一個安全隱患,如果有人想要在表單中注入某種代碼,他們可能可以。

希望您的代碼能夠區分圖像和腳本。

相關問題