I am using AJAX to retrieve the ID of a certain HTML element. The HTML ID is constructed like "sqlitem_1", "sqlitem_2", "sqlitem_3" etc. and each number corresponds to a record in the database.
I tried preg_replace('/\\D/', '', $item);
where $item
is the string I need to cut, but this didn't do the trick.
Please note that preg_replace
doesn't change the argument but rather returns the new value. You might wanna check the preg_replace manual. So what you need to do is assign the returned value
$item = preg_replace('/\D/', '', $item);
instead.
This could be done either server side (PHP) or if you wanted to preserve the variable name you could use the JavaScript Split function.
JS Split Function (Client Side)
var myVar = "sqlitem_1";
var tmp = str.split("_");
and then access each element using tmp[0] etc
https://www.w3schools.com/jsref/jsref_split.asp
OR
PHP (Server Side)
$ITEMTMP=explode("_", $item);
$itemnumber=$ITEMTMP[1];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.