当前我知道的唯一全局php命令是: 这个吐口水: 1 item(s) - £318.75 我想获得318.75值,所以目前我正在尝试替换,但它不能顺利使用: $short = $text_items; $short = str_replace("£", "", $short); $short = str_replace("£", "", $short); $short = str_replace("-", "", $short); $short = str_replace("–", "", $short); $short = str_replace(" ", "", $short); $short = str_replace("-", "", $short); $short = str_replace("ITEMS", "", $short); $short = str_replace("(", "",
以下是关于 substr 的编程技术问答
我有这样的数组,我会非常简单地理解 $picture = ( 'artist2-1_thumb.jpg', 'artist2-2.jpg' , 'artist2-3_thumb.jpg', 'artist2-4.jpg', 'artist2-5_thumb.jpg'); 现在,我希望使用substr获得只有拇指的新数组,才能具有这样的新数组 $picturethumbs = ( 'artist2-1_thumb.jpg', 'artist2-3_thumb.jpg', 'artist2-5_thumb.jpg'); 可以从哪里开始? 解决方案 您可以使用 array_filter() 要过滤数组,仅返回与给定条件匹配的项目: $picturethumbs = array_fil
处理我显示的图像的PHP文件仅允许一种图像格式,.jpg,.png,.bmp等,但不是全部. Imagename存储数据库中存储的图像的文件名,包括其格式.这是我的代码,到目前为止尚未工作,我不确定是否允许.你能帮我修理吗? $con = mysqli_connect("localhost","root","","tickets"); $ticket = 109; $result = mysqli_query($con,"SELECT image, imageName FROM tix WHERE tktNum=$ticket"); while($row = mysqli_fetch_array($result)) { $image = $row['image']; $imageName = $row['imageName']; $format = substr( $imageName, -3 ); //gets the last 3 chars of
我读了其他一些问题,尝试了答案,但最后没有结果.我得到的是这样的 Μήπως θα έπρεπε να � ... 我无法删除那个奇怪的问号.我要做的是获取编码的RSS提要的内容 使用希腊语言作为内容. 有什么方法可以解决此问题?
description = strip_tags($entry->description); echo mb_substr($entry->description, 0, 490); ?> ... 解决方案 这是答案 mb_substr($entry->description, 0, 490, "UTF-8"); 其他解决方案 我相信问题是您的编
当我在php中使用substr()函数时,我会在字符串末尾得到一个问号(取决于浏览器),当时最后一个字符是特殊字符时,例如ë或Ö等... $introtext = html_entity_decode($item->description, ENT_QUOTES, "UTF-8"); $introtext = substr($introtext, 0, 200); 如何逃脱? 解决方案 如果您的字符串具有多重编码(如UTF-8),则应使用 mb_substr 避免这样的问题: $introtext=mb_substr($introtext,0,200); 其他解决方案 以防有人尝试过以前的答案,但仍然没有起作用: 尝试在mb_substr中添加一个Unicode名称,例如: $introtext = mb_substr($introtext, 0, 200, 'utf-8'); 其他解决方案 使用 mb_substr
substr()返回28个字符而不是25个字符. $nature_goods是输入字段,用户可以在其中输入包含特殊字符的字符串. 代码 $nature_goods = "Nature quantityyy of goods is nice 120pcs 1*X23X24898"; echo strlen($nature_goods); echo "
"; echo substr($nature_goods, 0,25); echo "
"; echo strlen(substr($nature_goods, 0,25)); echo "
"; echo substr($nature_goods, 25,50); echo "
"; echo strlen(substr($nature_goods, 25,50)); echo "
"; 输出 53 Nature quantityyy of good 25 s is nice 12
我有这个字符串:$str = "(he+is+genius*2)/clever";在数组中看起来像这样; Array ( [0] => ( [1] => he [2] => + [3] => is [4] => + [5] => genius [6] => ) [7] => * [8] => and [9] => / [10] => clever ) 我想做的是将美元标志$放在$str中存在的每个字符串之前,但忽略了非all-Alphanumeric和数字. 最后,我想拥有看起来像这样的东西; $newstr = "($he+$is+$genius*2)/$clever"; 解决方案 对于每个值,检查第一个char(或整个值)是否由ctype_alpha的字符制成,然后使用$预处理: // $arr is your array as defined in your question forea
我想从php中的字符串中提取数字如下: 如果 string ='make1to6'我想在整个字符串中提取'to''substring之前和之后的数字字符.即 1和6要提取 我将使用这些返回的值进行一些计算."我想在整个字符串中的" to"子字符串之前和之后提取数字字符.即1和6要提取 字符串的长度不是固定的,可以是10个字符的最大长度.该数字在字符串中的" to"的任一侧最大两个数字. . 一些示例字符串值: sure1to3 ic3to9ltd anna1to6 joy1to4val make6to12 ext12to36 想到: function beforeTo(string) { return numeric_value_before_'to'_in_the_string; } function afterTo(string) { return numeric_value_after_'to'_in_the_st
$theExcerpt = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis' $theExcerptAppend = (strlen($theExcerpt) > 156) ? '...' : ''; $theExcerpt = preg_replace('/\s+?(\S+)?$/', '', substr($theExcerpt, 0, 156)); $theExcerpt .= $theExcerptAppend; 只要输入短语长度超过156个字符,脚本就可以正常工作.但是,当长度小于156(如在154时)时,即使包括单词在内的字符串仍然小于156. 注意:我不希望字符串在单词中间终止,但是如果包含该单词不超过15
我正在使用下面的getExcerpt()函数来动态设置文本片段的长度.但是,我的subtr方法当前基于字符数.我想将其转换为单词计数.我需要分开函数还是可以使用php方法代替substr? function getExcerpt() { //currently this is character count. Need to convert to word count $my_excerptLength = 100; $my_postExcerpt = strip_tags( substr( 'This is the post excerpt hard coded for demo purposes', 0, $my_excerptLength ) ); return ": ".$my_postExcerpt."
我想知道是否有一种优雅的方法来修剪一些文字,但是在html标签时会知道? 例如,我有这个字符串: $data = 'some title text here that could get very long'; ,假设我需要在页面上返回/输出此字符串,但希望它不超过x字符.假设35在此示例中. 然后我使用: $output = substr($data,0,20); ,但现在我最终得到了: some title text here that 您可以看到关闭的强标被丢弃,从而破坏了HTML显示. 有办法解决吗?另请注意,例如,字符串中可以具有多个标签:
some text here and here
解决方案 几个月前,我创建了一个特殊功能,可以解决您的问题. 这是一个函数: function substr_close_
我在这里有此代码: $imagePreFix = substr($fileinfo['basename'], strpos($fileinfo['basename'], "_") +1); 这使我在下划线之后给我一切,但是我希望在下划线之前获得所有内容,我将如何调整此代码以在下划线之前获取所有内容? $fileinfo['basename']等于'feature_00' 谢谢 解决方案 您应该简单使用: $imagePreFix = substr($fileinfo['basename'], 0, strpos($fileinfo['basename'], "_")); 我没有看到使用explode并创建额外数组来获取第一个元素的理由. 您也可以使用(在PHP 5.3+中): $imagePreFix = strstr($fileinfo['basename'], '_', true); 其他解决方案 如果您完全确定总是有至少
我正在编写PHP中的解析器,该解析器必须能够处理大型内存字符串,因此这是一个重要的问题. (即,请不要"过早优化"我的火焰) substr功能如何工作?它是在内存中制作第二个字符串数据的副本,还是引用原始数据?我应该担心打电话,例如$str = substr($str, 1);在循环中吗? 解决方案 要进一步的评论,您的代码将同时在内存中同时使用两个字符串(完整的字符串和全米斯第一字符)(尽管不是由于作业为作业乍得说).请参阅: $string = str_repeat('x', 1048576); printf("MEM: %d\nPEAK: %d\n", memory_get_usage(), memory_get_peak_usage()); substr($string, 1); printf("MEM: %d\nPEAK: %d :-(\n", memory_get_usage(), memory_get_peak_usage()); $strin
为了能够模拟PHP和MySQL的substr,我想做 之类的事情 select * from table where last_four_chars(field) = '.png' 解决方案 文档一个与此直接相关的示例. select * from table where SUBSTRING(field, -4) = '.png' 其他解决方案 具有正确的功能: SELECT RIGHT('abcdefg', 3); -- efg 您可以通过子字符串获得相同的结果: SELECT SUBSTRING('abcdefg', -3); -- efg 其他解决方案 这个快速查询: Select * from table where picName like '%.png'
我到处搜索,但找不到适合我的解决方案. 我有以下内容: $bedroom_array = array($studio, $one_bed, $two_bed, $three_bed, $four_bed); 对于此示例,请说: $studio = '1'; $one_bed = '3'; $two_bed = '3'; i然后使用爆破函数在所有值之间放置一个逗号: $bedroom_list = implode(", ", array_filter($bedroom_array)); echo $bedroom_list; 然后输出: 1,2,3 我想做的是在字符串中找到最后一个逗号,然后用AN&替换为AN&,因此会读取: 1、2和3 字符串并不总是那么长,可以更短或更长,例如1、2、3、4等.我已经考虑使用substr,但不确定这是否适合我的需求? 解决方案 弹出最后一个元素,将其余的插入最后一个,然后将最后一个插入.
当我使用substr()时,我在末尾得到一个奇怪的角色 $articleText = substr($articleText,0,500); 我的输出为500个字符, 我该如何解决?这是一个编码问题吗?我的语言是希腊语. 解决方案 substr 使用字节而不是字符计数. 希腊语可能意味着您正在使用一些多字节编码,例如utf-8-而计数每个字节对这些字节不太好. 也许使用 mb_substr 可以帮助,这里: mb_* 已专门为多字节编码创建了功能. 其他解决方案 使用 mb_substr 相反,它能够处理多个编码,不仅是单个编码字节字符串如 substr : $articleText = mb_substr($articleText,0,500,'UTF-8'); 其他解决方案 看起来您将Unicode字符切成一半.使用 mb_substr 而不是用于Unicode-Safe String Slicing.