html中怎么添加字符串,如何在HTML片段中插入字符串

我不知道这个源码究竟来自哪里,但我一直在使用这段代码,它非常出色。

/**

* @desc Cut given plain/HTML text nicely

* @param string text to cut

* @param int approximetly length of desired text length

* @param int optional length, how far text can variante from approximetly length

* @param bool optional can we cut words

* @param bool optional do we need to append three dots to the end of cutted text

* @return string cutted text

*/

function htmlSubstr($text, $approxLength, $lengthOffset = 20, $cutWords = FALSE, $dotsAtEnd = TRUE) {

mb_internal_encoding('UTF-8');

// $approxLength:

// The approximate length you want the concatenated text to be

// $lengthOffset:

// The variation in how long the text can be in this example text

// length will be between 200 and 200-20=180 characters and the

// character where the last tag ends

// Reset tag counter & quote checker

$tag_counter = 0;

$quotes_on = FALSE;

// Check if the text is too long

if (mb_strlen($text) > $approxLength) {

// Reset the tag_counter and pass through (part of) the entire text

$c = 0;

for ($i = 0; $i < mb_strlen($text); $i++) {

// Load the current character and the next one

// if the string has not arrived at the last character

$current_char = mb_substr($text,$i,1);

if ($i < mb_strlen($text) - 1) {

$next_char = mb_substr($text,$i + 1,1);

} else {

$next_char = "";

}

// First check if quotes are on

if (!$quotes_on) {

// Check if it's a tag

// On a "

// or add only 1 if it's an ending tag (like )

if ($current_char == '

if ($next_char == '/') {

$tag_counter += 1;

} else {

$tag_counter += 3;

}

}

// Slash signifies an ending (like or ... />)

// substract 2

if ($current_char == '/' && $tag_counter <> 0) $tag_counter -= 2;

// On a ">" substract 1

if ($current_char == '>') $tag_counter -= 1;

// If quotes are encountered, start ignoring the tags

// (for directory slashes)

if ($current_char == '"') $quotes_on = TRUE;

} else {

// IF quotes are encountered again, turn it back off

if ($current_char == '"') $quotes_on = FALSE;

}

// Count only the chars outside html tags

if($tag_counter == 2 || $tag_counter == 0) $c++;

// Check if the counter has reached the minimum length yet,

// then wait for the tag_counter to become 0, and chop the string there

if ($c > $approxLength - $lengthOffset && $tag_counter == 0 && ($next_char == ' ' || $cutWords == TRUE)) {

$text = mb_substr($text,0,$i + 1);

if($dotsAtEnd){

$text .= '...';

}

return $text;

}

}

}

return $text;

}

在C语言,在顺序表(通常是字符数组或动态内存分配的字符数组)插入一个字符串通常涉及到以下几个步骤: 1. **计算新表长度**: - 首先,你需要获取当前列表字符串的数量以及每个字符串占用的字节数(包括结束符'\0')。如果列表为空,新长度初始化为1;否则加上现有字符串的长度。 2. **动态扩容(如有必要)**: - 如果新的字符串将导致总长度超过当前数组的大小,需要创建一个新的更大的数组。使用`realloc()`函数来动态调整内存大小,把原有数据移动到新的位置,并保留足够的空间放新字符串。 3. **插入字符串**: - 使用`strcpy()`或`memcpy()`函数将新字符串复制到数组的新位置。确保留出足够空间,比如从最后一个元素的位置开始算起,加上新字符串的长度。 4. **更新指针和长度**: - 更新指向列表的指针(如`head`),使其指向新插入字符串。 - 调整总长度变量,表示现在有更多字符串。 5. **添加结束符**: - 在新字符串的结尾添加`\0`,确保它作为一个独立的字符串存在。 下面是一个简单的示例代码片段: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义顺序表结构体(这里假设只有一个字符串) typedef struct { char* strings; int size; // 当前元素个数 int capacity; // 当前数组容量 } StringList; void insertString(StringList* list, const char* newStr) { // ... (以上步骤) int totalLength = strlen(list->strings) + strlen(newStr) + 1; // 加1是因为'\0' if (totalLength > list->capacity) { // 动态扩容 list->strings = realloc(list->strings, totalLength * sizeof(char)); if (list->strings == NULL) { // 内存分配失败处理 perror("Memory allocation failed"); return; } list->capacity *= 2; // 新增一倍容量 } strcpy(list->strings + list->size, newStr); // 将新字符串复制到适当位置 list->size++; // 更新元素个数 list->strings[list->size] = '\0'; // 添加结束符 } int main() { StringList myList; myList.strings = malloc(10 * sizeof(char)); // 初始容量为10 myList.size = 0; myList.capacity = 10; insertString(&myList, "Hello, C!"); // 插入字符串 // 现在你可以遍历并打印列表... for (int i = 0; i <= myList.size; i++) { printf("%s ", myList.strings[i]); } free(myList.strings); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值