【发布时间】:2015-08-14 01:12:32
【问题描述】:
我有一个复杂的横幅背景,文本融入其中,所以我需要为这个 div 添加一个背景,以便它显示给定的文本。有没有一种使用 PHP 的方法可以获取文本的长度并将其转换为 px 以便我可以使用它来设置 div 的宽度?
它是我想要自动的entry-title
HTML:
<div class="fusion-page-title-bar fusion-page-title-bar-none fusion-page-title-bar-left">
<div class="fusion-page-title-row">
<div class="fusion-page-title-wrapper">
<div class="fusion-page-title-captions">
<h1 class="entry-title" data-fontsize="31" data-lineheight="NaN">Cheats Hidden Vegetables</h1>
</div>
</div>
</div>
</div>
功能:
function avada_page_title_bar( $title, $subtitle, $secondary_content ) {
global $smof_data;
$post_id = get_queried_object_id();
// Check for the secondary content
$content_type = 'none';
if ( strpos( $secondary_content, 'searchform' ) !== FALSE ) {
$content_type = 'search';
} elseif ( $secondary_content != '' ) {
$content_type = 'breadcrumbs';
}
// Check the position of page title
if ( metadata_exists( 'post', $post_id, 'pyre_page_title_text_alignment' ) &&
get_post_meta( get_queried_object_id(), 'pyre_page_title_text_alignment', TRUE ) != 'default'
) {
$alignment = get_post_meta( $post_id, 'pyre_page_title_text_alignment', TRUE );
} elseif ( $smof_data['page_title_alignment'] ) {
$alignment = $smof_data['page_title_alignment'];
}
// Render the page title bar
echo sprintf( '<div class="fusion-page-title-bar fusion-page-title-bar-%s fusion-page-title-bar-%s">', $content_type, $alignment );
echo '<div class="fusion-page-title-row">';
echo '<div class="fusion-page-title-wrapper">';
echo '<div class="fusion-page-title-captions">';
if( $title ) {
// Add entry-title for rich snippets
$entry_title_class = '';
if ( ! $smof_data['disable_date_rich_snippet_pages'] ) {
$entry_title_class = ' class="entry-title"';
}
echo sprintf( '<h1%s>%s</h1>', $entry_title_class, $title );
if ( $subtitle ) {
echo sprintf( '<h3>%s</h3>', $subtitle );
}
}
// Render secondary content on center layout
if ( $alignment == 'center') {
if ( fusion_get_option( 'page_title_bar_bs', 'page_title_breadcrumbs_search_bar', $post_id ) != 'none' ) {
echo '<div class="fusion-page-title-secondary">';
echo $secondary_content;
echo '</div>';
}
}
echo '</div>';
// Render secondary content on left/right layout
if ( $alignment != 'center' ) {
if ( fusion_get_option( 'page_title_bar_bs', 'page_title_breadcrumbs_search_bar', $post_id ) != 'none' ) {
echo '<div class="fusion-page-title-secondary">';
echo $secondary_content;
echo '</div>';
}
}
echo '</div>';
echo '</div>';
echo '</div>';
}
}
【问题讨论】:
-
为什么是
PHP?为什么不使用 JavaScript? -
你不能用 php 来判断,因为它取决于文本的样式。 javascript可以做你想做的事
-
strlenphp.net/manual/en/function.strlen.php “有没有办法使用PHP获取文本的长度” -
@Fred-ii- 你不会根据 strlen 对元素大小做出假设。即使不同的浏览器有时会呈现不同的布局,即使它们安装了主要字体,甚至没有提到不同的设备。坚持使用可以考虑元素实际 RENDERED 大小的 CSS 或 JS+CSS 解决方案。
-
@DanielBrose 假设基于无代码,并且 OP 在问题中提出了它; 回答。如何在 PHP 中检查字符串长度。编辑:啊,现在他们展示了代码。
标签: php content-length