0

特殊文字のジャンクアップされたデータ(いくつかを許可)をクリーンアップしようとしていますが、それでも成功するものもあります。以前に正規表現スニペットを見つけましたが、アスタリスクなどの一部の文字は削除されません。

  $clean_body = $raw_text;

  $clean_title = preg_replace("/[^!&\/A-Za-z0-9_ ]/","", $clean_body);
  $clean_title = substr($clean_title, 0, 64);

  $clean_body = nl2br($clean_body);  

  if ($nid) {
    $node = node_load($nid);
    unset($node->field_category);
  } else {
    $node = new stdClass();
    $node->type = 'article';
    node_object_prepare($node); 
  }

  $split_title = str_split($clean_title);

  foreach ($split_title as $key => $character) {
    if ($key > 15) {
      if ($character == ' ' && !preg_match("/[^!&\/,.-]/", $split_title[$key - 1])) {
        $node->title = html_entity_decode(substr(strip_tags($clean_title), 0, $key - 1)) . '...';
      }
    }
  }

最初の部分は、通常の句読点や英数字ではない生のテキスト内のすべてをクリーンアップしようとします。次に、タイトルを配列に分割し、スペースを探します。私がやりたいのは、15文字以上の長さのタイトルを作成し、句読文字で停止することなく、スペースで切り捨てる(単語全体をそのままにする)ことです。これは私が問題を抱えている部分です。

一部のタイトルは、最初のタイトルに'が含まれていてはならず、セクションがたとえばである場合でも、*****************またはとして表示されます。** HOW TO MAKE $$$$$$ BLOGGING ***HOW TO MAKE...

4

2 に答える 2

0

Your problem (or, one of them anyhow) is this logic:

if ($key > 15) {
  if ($character == ' ' && !preg_match("/[^!&\/,.-]/", $split_title[$key - 1])) {
    $node->title = html_entity_decode(substr(strip_tags($clean_title), 0, $key - 1)) . '...';
  }
}

You're only setting $node->title if these conditions match when iterating the characters in the $split_title array.

What happens when they don't match? $node->title doesn't get set (or overwritten? You didn't give much context, so I can't tell).

Using this as a test:

$clean_body = '** HOW TO MAKE $$$$$$ BLOGGING **';

You can see that these conditions do not match, so $node->title does not get set (or overwritten).

于 2011-09-13T17:04:09.567 に答える
0

どう"/[^!&\/\w\s]/ui"ですか?私のマシンで正常に動作します

于 2011-09-13T16:41:20.430 に答える