简体   繁体   English

如何从输入字段中获取特定值

[英]How to get a particular value from input field

My user can add his any audio from reverbnation.com. 我的用户可以从reverbnation.com添加他的任何音频。 So user can copy his embed code and paste it my form input. 因此,用户可以复制其嵌入代码并将其粘贴到我的表单输入中。 I want my form get only two word from input text. 我希望我的表单从输入文本中仅得到两个单词。

For example:- reverbnation embad code like as: 例如:-混响嵌入代码如下:

<iframe class="widget_iframe" src="https://www.reverbnation.com/widget_code/html_widget/artist_4859830?widget_id=50&amp;posted_by=label_265034&pwc[design]=customized&pwc[background_color]=%23d4c79f&pwc[included_songs]=0&pwc[song_ids]=23591473&pwc[photo]=0%2C1&pwc[size]=undefined" width="100%" height="100%" frameborder="0" scrolling="no"></iframe>

So if my user paste above embed code in form input field, I want to collect two word from above text to save it my sql database. 因此,如果我的用户将上面的嵌入代码粘贴到表单输入字段中,那么我想从上面的文本中收集两个单词以将其保存在我的sql数据库中。

  1. Artist Id = 4859830 (...artist_4859830?...) 艺术家ID = 4859830 (... artist_4859830?...)
  2. Song Id = 23591473 (...&pwc[song_ids]=23591473...) 歌曲ID = 23591473 (...&pwc [song_ids] = 23591473 ...)

Please give me a guideline; 请给我一个指导; How to collect above 2 id from input field. 如何从输入字段中收集2个以上ID。

My sql database: id | 我的sql数据库:id | userid | 用户名| artist | 艺术家| song 歌曲

my form page: 我的表单页面:

if(isset($_POST['submit'])){
    $code = mysqli_real_escape_string($dbh, $_POST['code']);
    $userid = mysqli_real_escape_string($dbh, $_POST['userid']);

    $artist = How to get artist id (4859830) from $code 
    $song = How to get song id (23591473) from $code

    $result = mysqli_query($dbh,"INSERT INTO user_aideo (id, userid, artist, song) values('', '$userid', '$artist', '$song')");
}

<form id="form" method="post" action="">
   <input type="text" class="form-control" name="code"/>
   <input type="hidden" name="userid" value="<? echo $session->userid; ?>"/>
   <input type="submit" name="submit" id="submit" value=" Submit "/>
</form>

I would argue that this would be a good use case for regular expressions . 我认为这对于正则表达式将是一个很好的用例。

For your particular scenario, you can capture both numbers in one execution. 对于您的特定方案,您可以一次执行捕获两个数字。 You just need to use the preg_match function. 您只需要使用preg_match函数。

if (preg_match("/(?:.*)artist_([0-9]*)\?(?:.*)song_ids]=([0-9]*)/i", $code, $matches)) {
    print 'artist: ' . $matches[1] . '<br />';
    print 'song: ' . $matches[2];
}

This works with your given code example. 这适用于您给定的代码示例。 It will put the artist number in $matches[1] and the song number in $matches[2] . 它将歌手编号放在$matches[1] ,而歌曲编号放在$matches[2]

I'm comfortable directly referencing these in an if statement because the regular expression has two capturing groups, so there will always be 3 indexes in the $matches array if preg_match returns true . 我很乐意直接在if语句中引用它们,因为正则表达式具有两个捕获组,因此如果preg_match返回true ,则在$matches数组中将始终有3个索引。 If you make a direct call to preg_match outside of an if statement, you will want to loop through the $matches array to avoid an undefined index error. 如果在if语句之外直接调用preg_match ,则需要遍历$matches数组,以避免未定义的索引错误。

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

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