| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
/** |
| 6 |
* A helper object for string operations. |
| 7 |
*/ |
| 8 |
class String_Helper { |
| 9 |
|
| 10 |
/** |
| 11 |
* Strips all HTML tags including script and style. |
| 12 |
* |
| 13 |
* @param string $text The text to strip the tags from. |
| 14 |
* |
| 15 |
* @return string The processed string. |
| 16 |
*/ |
| 17 |
public function strip_all_tags( $text ) { |
| 18 |
return \wp_strip_all_tags( $text ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Standardize whitespace in a string. |
| 23 |
* |
| 24 |
* Replace line breaks, carriage returns, tabs with a space, then remove double spaces. |
| 25 |
* |
| 26 |
* @param string $text Text input to standardize. |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
public function standardize_whitespace( $text ) { |
| 31 |
return \trim( \str_replace( ' ', ' ', \str_replace( [ "\t", "\n", "\r", "\f" ], ' ', $text ) ) ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* First strip out registered and enclosing shortcodes using native WordPress strip_shortcodes function. |
| 36 |
* Then strip out the shortcodes with a filthy regex, because people don't properly register their shortcodes. |
| 37 |
* |
| 38 |
* @param string $text Input string that might contain shortcodes. |
| 39 |
* |
| 40 |
* @return string String without shortcodes. |
| 41 |
*/ |
| 42 |
public function strip_shortcode( $text ) { |
| 43 |
return \preg_replace( '`\[[^\]]+\]`s', '', \strip_shortcodes( $text ) ); |
| 44 |
} |
| 45 |
} |
| 46 |
|