简体   繁体   English

如何在字符串中用方括号替换引号

[英]How do I replace quotes with square brackets in a string

I have this string 'My String' with square brackets eg [My String] . 我有带方括号的字符串'My String' ,例如[My String]

At the moment I can only get the string like: 目前,我只能得到如下字符串:

$re = '/'([^']+)'/'; $str = "'My String'"; preg_match($re, $str, $matches);

How can I get replace the qoutes using preg_replace() 我如何使用preg_replace()替换qoutes

You can use this code to replace '' with []: 您可以使用以下代码将“”替换为[]:

<?php

$string = "blabla'My String'blabla";
echo preg_replace("/([^']*)'([^']+)'([^']*)/", '$1[$2]$3', $string);
// Output will be "blabla[My String]blabla"

preg_replace is a core php function that replace a pattern in a string preg_replace是一个核心php函数,用于替换字符串中的模式

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

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