试图获取一个具有一个子字符串静态和一个子字符串动态的字符串


Trying to grab a string with one substring static, and one dynamic

我正试图找到获取动态子字符串的最佳方法,但之后会替换所有内容。

这就是我正在努力实现的目标:

{table_telecommunications}

子串{table_总是相同的,唯一变化的是telecommunications}

我想获取单词telecommunication,这样我就可以在MySQL表上进行搜索,然后用返回的内容替换{table_telecommunications}

我想做一个strpos,然后做explode等等

但我想使用regex会更容易,但我没有创建regex的技能。

你能给我最好的方法吗?

编辑:我说regex可能是最好的方法,因为我需要找到这种格式的字符串,但第二部分是可变的,就像{table_*}

使用Regex。

<?php
    $string = "{table_telecommunications} blabla blabla {table_block}";
    preg_match_all("/'{table_(.+?)'}/is", $string, $matches);
    $substrings = $matches[1];
    print_r($substrings);
?>
if (preg_match('#table_([^}]+)}#', '{table_telecommunications}', $matches)){
    echo $matches[1];
}

这是一个正则表达式解决方案。你可以用爆炸做同样的事情:

$parts = explode('table_', '{table_telecommunications}');
echo substr($parts[1], 0, -1);
$input = '{table_telecommunications}';
$table_name = trim(implode('_', array_shift(explode($input, '_'))), '}');

应该很快,不需要正则表达式。