admin管理员组文章数量:1122846
I'm looking to filter numbers from a string, and make them bigger in one case, and hide them in the other. The best way, as I understood, would be to use a custom function via functions.php and probably Javascript. The string is using a unique div class.
Here's how it looks in HTML:
<div class="big-numbers">15th Amendment</div>
what I think would be a good result to work with in CSS later on:
<div class="big-numbers"><span>15</span>th Amendment</div>
I previously used a code to filter the first word, but I cannot use it here due to ordinal numbers, and not sure how can I filter just numbers.
I'm looking to filter numbers from a string, and make them bigger in one case, and hide them in the other. The best way, as I understood, would be to use a custom function via functions.php and probably Javascript. The string is using a unique div class.
Here's how it looks in HTML:
<div class="big-numbers">15th Amendment</div>
what I think would be a good result to work with in CSS later on:
<div class="big-numbers"><span>15</span>th Amendment</div>
I previously used a code to filter the first word, but I cannot use it here due to ordinal numbers, and not sure how can I filter just numbers.
Share Improve this question asked Aug 29, 2024 at 20:29 MajtresMajtres 1 2- the string you want to parse comes from an editor ? – mmm Commented Aug 30, 2024 at 0:50
- it's ACF field, but it shouldn't matter right? – Majtres Commented Aug 31, 2024 at 13:58
1 Answer
Reset to default 0You could use PHP's preg_replace()
to wrap all the numbers (ie, any successive combination of 0
through 9
) in a <span>
tag.
add_filter( 'the_content', 'wpse426625_number_wrapper' );
/**
* Wraps any number in the content in a <span> tag.
*
* @param string $content The post content.
* @return string The content with the numbers wrapped in <span>s.
*/
function wpse426625_number_wrapper( $content ) {
$content = preg_replace(
'/(\d+)/', // \d is any digit; + matches one or more.
'<span class="numbers">$1</span>', // $1 is the text inside () in the search.
$content
);
return $content;
}
This assumes you want to wrap all the numbers in WordPress's post content in <span>
tags; I've also added a class
to the spans so you can target them with CSS. If my assumptions are incorrect, I recommend you edit your question to clarify what you're looking for.
Also note: this question is really a PHP question (or possibly a Javascript question), not a WordPress question. I've answered it here, but it more properly belongs on Stack Overflow.
Regular expressions (such as those used by preg_replace()
) are very powerful; if you haven't encountered them before, you'd do well to look into how they work.
本文标签: javascriptFunction to filter numbers from string
版权声明:本文标题:javascript - Function to filter numbers from string 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736296328a1929768.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论