-1

Не получается избавиться от символа ' . Выдаёт то ромбики со знаками вопроса то код. Уже всё перепробовал .

if ( $terms ) {
    $i = 2;
    printf( '<div class="%s">
        <h2 class="text-center">Select one of the functional areas</h2>
        <ul class="clearfix">', esc_attr( $class ) );
      printf( '<li class="'.$activet.'" ><a  href="%s">All categories</a><div class="description">All categories on one page</div></li>', esc_url( site_url() ));

        foreach ( $terms as $term ) {

            $active ='';
            // The $term is an object, so we don't need to specify the $taxonomy.
            $term_link = get_term_link( $term );
            $term_name = esc_html( $term->name );
            $clean = preg_replace("/[^\w]/", "", $term_name);
            $name = "/\b".$clean."\b/i";
            if (preg_match($name,$link) == true && preg_match($name,$url) == true) {
            $active = 'active-taxonomy';

            } else {
            $active ='';
            } 
            // If there was an error, continue to the next term.
            if ( is_wp_error( $term_link ) ) {
                continue;
            }
            printf( '<li class="'.esc_html( $term->name ).' '.$active.'" ><a href="%s">%s</a><div class="description">%s</div></li>', esc_url( $term_link ), esc_html( $term->name ),esc_html( $term->description ) );
            if (($i % 3) === 0) {
                echo '<li class="clear">&nbsp;</li>';
            }
            $i++;
        }
    print( '</ul></div>' );
}

Если делать вот так:

    if ( $terms ) {
    $i = 2;
    printf( '<div class="%s">
        <h2 class="text-center">Select one of the functional areas</h2>
        <ul class="clearfix">', esc_attr( $class ) );
      printf( '<li class="'.$activet.'" ><a  href="%s">All categories</a><div class="description">All categories on one page</div></li>', esc_url( site_url() ));

        foreach ( $terms as $term ) {

            $active ='';
            // The $term is an object, so we don't need to specify the $taxonomy.
            $term_link = get_term_link( $term );
            $term_name = $term->name;
            $clean = preg_replace("/[^\w]/", "", $term_name);

            $name = "/\b".$clean."\b/i";
            $low = strtolower($name);
            $link = strtolower($term_link);
            if (preg_match($name,$link) == true && preg_match($name,$url) == true) {
            $active = ' active-taxonomy';
            } else {
            $active ='';
            }
            // If there was an error, continue to the next term.
            if ( is_wp_error( $term_link ) ) {
                continue;
            }

            printf( '<li class="'.$low.$active.'" ><a href="%s">%s</a><div class="description">%s</div></li>', esc_url( $term_link ), esc_html( $term->name ),esc_html( $term->description ) );
            if (($i % 3) === 0) {
                echo '<li class="clear">&nbsp;</li>';
            }
            $i++;
        }
    print( '</ul></div>' );
}

То выдает - ��hildrensroom . Я вообще не могу понять в таком случае. Буду рад любой помощи.

0

2 ответа 2

1

Может потому что вы производите замену в другой переменной? попробуйте так:

    $h = "Man's horse"; 
    $replace = preg_replace("/[^\w]/", "", $h);
    var_dump($replace);
8
  • Да это опечатка . Не продвинулся с помощью var_dump : string(17) "Сhildren039sroom" . Проблема в самом регулярном выражении . Не могу понять как указать именно этот символ , чтобы его не было на выводе. 17 авг 2016 в 0:31
  • $h = preg_replace("/[^\w]/",'',$term_name); $clean = preg_replace("/[^a-zA-Z]/",'',$h); 17 авг 2016 в 1:35
  • @Andrei Sukharev, код из ответа выдаст string(9) "Manshorse". Вы избавились и от одинарной кавычки, и от пробела.
    – Visman
    17 авг 2016 в 2:21
  • всё верно. Наверное у меня конфликт функций вордпресса с php. Может посмотрите на код выше - я привёл варианты и результаты. 17 авг 2016 в 12:54
  • 1
    @AndreiSukharev так попробуйте такое регулярное выражение [^\w\s] - удалит все кроме букв и пробелов 17 авг 2016 в 13:04
0

Стринг реплайс не

str_replace("'","",$h);

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.