preg_replace不应切出单词

时间:2018-10-01 18:50:05

标签: php preg-replace

我的代码:

$slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($_POST["title"])));

例如,当我写:Úm Titulo时,我得到:-m-titulo。如您所见,我错过了Ú。当我写的时候:Úm Titulo。我应该得到um-titulo。如何解决? preg_replace切勿强调单词。

2 个答案:

答案 0 :(得分:0)

exports.cardPay = functions.https.onRequest((req, res) => { return cors(req, res, () => { const cart = req.body.cart const user = req.body.verifiedUserLogged const key = admin.database().ref(`sales/${user}`).push().key processCart(cart).then(result => { console.info(createPayment(result, key)) return res.json({ "data": createPayment(result, key) }).end() }).catch(console.log) }) }) function processCart(cart) { return new Promise((resolve, reject) => { Promise.all(cart.map(i => switcher(i))) .then(prices => resolve( prices.reduce( (finalPrice, price) => price + finalPrice, 0) )).catch(console.log) }); } function switcher(item) { switch (item.destiny) { case 'bookedLessons': return lessonPrice(item.name, item.index) case 'bookedRentals': return rentalPrice(item.id, item.index, item.insurancePurchased, item.insuranceId) case 'bookedLodgins': return item.reservationData ? roomPriceWithReservation(item.id, item.quantity, item.persons, item.reservationData) : roomPriceNoReservation(item.id, item.quantity, item.persons) case 'deliveries': return productPrice(item.id, item.quantity) case 'bookedCar': return carPrice(item.id, item.index) case 'bookedStorage': return storagePrice(item.index) case 'bookedTransportation': return transportationPrice(item.id, item.index, item.persons, item.roundTrip) case 'bookedDoublePack': return doublePack(item.id, item.persons) case 'bookedTriplePack': return triplePack(item.id, item.persons) default: break } } function createPayment(total, orderId) { let redsys = new Redsys(); let mParams = { "DS_MERCHANT_AMOUNT":total.toString(), "DS_MERCHANT_ORDER":orderId, "DS_MERCHANT_MERCHANTCODE": "025988262", // "DS_MERCHANT_MERCHANTCODE":tpvInfo.fucCode, "DS_MERCHANT_CURRENCY":"978", // "DS_MERCHANT_CURRENCY":tpvInfo.currency, "DS_MERCHANT_TRANSACTIONTYPE":"0", // "DS_MERCHANT_TRANSACTIONTYPE":tpvInfo.transaction_type, "DS_MERCHANT_TERMINAL": "001", // "DS_MERCHANT_TERMINAL":tpvInfo.terminal, "DS_MERCHANT_MERCHANTURL":'http://localhost:8080', "DS_MERCHANT_URLOK":'http://localhost:8080/home?foo=true', "DS_MERCHANT_URLKO":'http://localhost:8080/home?foo=false' }; return {signature: redsys.createMerchantSignature(/* tpvInfo.secret */ "sq7HjrUOBfKmC576ILgskD5srU870gJ7", mParams) , merchantParameters: redsys.createMerchantParameters(mParams), raw: mParams}; } 模式匹配1个以上非ASCII字母和数字的字符。它与/[^a-z0-9]+/iЯёł和更多字母匹配。

您可以使用

ę

请参见regex demo

在这里,preg_replace('~[\W_]+~u', '-', $s) 匹配非Unicode字母或数字的任何字符([\W_]不匹配\W,因此_被添加到字符类中)

u modifier使_能够识别Unicode。

也要将所有Unicode字母更改为它们的基本形式,请对字符串进行标准化:

\W

注意:确保启用了 $result = strtolower( preg_replace('~[\W_]+~u', '-', normalizer_normalize($text, Normalizer::NFKC)) ); 扩展名。

答案 1 :(得分:0)

我认为问题出在您的正则表达式,因为它不考虑带有重音符号的字母? 请参阅以下内容:

Replacing Accented Characters