如何在特定字符串之前和之后获取文本

时间:2023-02-09 15:29:06

I have content stored in variables and I have a string that users enter in search fields.

我有内容存储在变量中,我有一个用户在搜索字段中输入的字符串。

My question is how can I chop the text before and after the searched string?

我的问题是如何在搜索到的字符串之前和之后剪切文本?

-SearchString has a the value which user has entered.

-SearchString具有用户输入的值。

-Wholetext has the whole data from database.

-Wholetext具有来自数据库的全部数据。

Now, I want to show the Wholetext as the excerpt like user search for "test" and then I will show the result like:

现在,我想将Wholetext显示为用户搜索“test”的摘录,然后我将显示如下结果:

"text before search string" : "test"

“搜索字符串前的文字”:“测试”

"text after search string" : "_"

“搜索字符串后的文字”:“_”

Here's my code:

这是我的代码:

{% block field %}
    <div>
        {% set SearchString=admin.datagrid.filters.data.value.value %}
        <div class="less_resume_container">
            {% if SearchString is defined and  SearchString in object.data %}
                {% set Wholetext=  object.data|replace({ (SearchString): '<span style="background-color: #FFFF00;font-size:15px;font-weight:bold">' ~SearchString~'</span>'}) %}
                {{ Wholetext|striptags()|truncate(50) }} <a href="javascript:void(0)" class="show_full_resume">Show
                more</a>
            {% else %}
                {% set  Wholetext=  object.data %}
                {{ Wholetext|truncate(50) }} <a href="javascript:void(0)" class="show_full_resume">Show more</a>
            {% endif %}
        </div>
        <div class="full_resume_container" style="display: none;">{{ Wholetext|raw() }}
            <a href="javascript:void(0)" class="show_less_resume">View less</a>
        </div>

        <br/>

    </div>
{% endblock %}

Currently it is showing 50 characters from the text. This is not what I want. By the way I am using sonata admin with symfony 2.

目前它显示文本中的50个字符。这不是我想要的。顺便说一句,我正在使用symfony 2的sonata admin。

1 个解决方案

#1


0  

You can use a combination of substr and strlen.

您可以使用substr和strlen的组合。

substr(WHOLETEXT, 0, (strlen(WHOLETEXT) - strlen(SEARCHTERM)))

Alternatively, you could explode the whole text on the search term, which would leave you with a two part array(assuming your search term only appeared once). The first part would be everything before the search term. The second part would be everything after.

或者,您可以在搜索词上分解整个文本,这会留下一个两部分数组(假设您的搜索词只出现一次)。第一部分是搜索词之前的所有内容。第二部分将是之后的一切。

$array = explode(SEARCHTERM, WHOLETEXT); 
$before = $array[0];
$after = $array[1];

#1


0  

You can use a combination of substr and strlen.

您可以使用substr和strlen的组合。

substr(WHOLETEXT, 0, (strlen(WHOLETEXT) - strlen(SEARCHTERM)))

Alternatively, you could explode the whole text on the search term, which would leave you with a two part array(assuming your search term only appeared once). The first part would be everything before the search term. The second part would be everything after.

或者,您可以在搜索词上分解整个文本,这会留下一个两部分数组(假设您的搜索词只出现一次)。第一部分是搜索词之前的所有内容。第二部分将是之后的一切。

$array = explode(SEARCHTERM, WHOLETEXT); 
$before = $array[0];
$after = $array[1];