看到PHP的一道面试题, 做了上, 不知道还有没好点方法
看到PHP的一道面试题, 做了下, 不知道还有没好点方法
题目如下,
类似这样的aaasssddd字符串,写个函数CutStr($str,$max)实现截取:
1)如果$max大于$str的长度则返回$str
2)和不包含在长度计算范围。
例:
如果CutStr("aaasssddd",6) 则返回aaasss.
如果CutStr("aaasssddd",4) 则返回aaas,不包含标签
我的第一反应就是正则, 然后就...
var str = "aaa<em>sss</em>ddd";
function cutStr(str, max) {
// 首先把<em>和</em>先给剔除, 然后记录他们的位置
var reg = new RegExp("(.*?)<em>(.*?)</em>(.*?)");
var emSub = str.indexOf("<em>");
var em2Sub = str.indexOf("</em>");
var newstr = str.replace(reg, "$1$2$3");
// 如果不是数字或是负数, 或者大于字符长度, 直接返回原字符
if (!/^\d+$/.test(max) || max >= newstr.length) return str;
newstr = newstr.substring(0, max);
if (max <= emSub) { //小于三
return newstr;
} else if (max <= em2Sub - 4 && max > emSub) { // 大于三, 小于六时(注: -4 是为了减去第一个<em>占去的位置)
var tempReg = new RegExp("(\\w{" + emSub + "})(\\w*?)");
return newstr.replace(tempReg, "$1<em>$2");
} else { // 大于六
var tempReg = new RegExp("(\\w{" + emSub + "})(\\w{" + (em2Sub - emSub - 4) + "}?)(\\w*?)");
return newstr.replace(tempReg, "$1<em>$2</em>$3");
}
}
alert(cutStr(str, 7));?
觉得这个方法好笨, 有什么别的好点的方法吗?
想到了,, 原来这个这么简单,, 我把它想复杂了...
var str = "aaa<em>sss</em>ddd";
function cutStr(str, max) {
var emSub = str.indexOf("<em>");
var em2Sub = str.indexOf("</em>");
// 如果不是数字或是负数, 或者大于字符长度, 直接返回原字符
if (!/^\d+$/.test(max) || max >= str.length - 9) return str;
else if(max > em2Sub-4) return str.substring(0, max + 9);
else if(max > emSub) return str.substring(0, max + 4);
else return str.substring(0, max);
}
alert(cutStr(str, 7)); ?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

iPhone 15 Pro vs. iPhone 14 Pro: Specs Comparison Here is a spec comparison between iPhone 15 Pro Max and iPhone 14 Pro Max: iPhone 15 Pro Max iPhone 14 Pro Max Display size 6.7 inches 6.7 inches Display technology Super Retina 2,000 nits Dimensions 6.29x3.02x0.32 inches 6.33x3.06x0.31 inches Weight 221 grams 240 grams

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

The latest iPhone Pro series is equipped with a powerful 48MP sensor that ensures highly detailed and crystal clear photos to capture every precious moment. However, one potential drawback is the size of full-resolution images, especially those in ProRAW format. Although the maximum storage space offered by the iPhone is 512GB, capturing a lot of ProRAW images (about 75MP each) and video (440MB per minute, 60FPS) can quickly eat up your storage space. This can cause problems if you plan to use your iPhone as your main camera for large projects or travel. But wouldn't it be great if you could take those high-resolution 48MP photos without worrying about storage limitations? it's quick

Although Apple will launch the iPhone's video playback time to let users know that the iPhone battery is almost available. But normal users don't use their iPhone to view videos all day long. 7 iPhones tested for endurance in daily applications. Includes 7 models including iPhone15ProMax, iPhone15Pro, iPhone15Plus, iPhone15, iPhone14ProMax, iPhone14 and iPhone13ProMax. Running through some daily applications, such as Spotify, Zoom, Tiktok, Headspace, think about Apps, games, etc., we can see the battery life of different iPhones. this

java8's stream takes maxpublicstaticvoidmain(String[]args){Listlist=Arrays.asList(1,2,3,4,5,6);Integermax=list.stream().max((a,b)->{if (a>b){return1;}elsereturn-1;}).get();System.out.println(max);}Note: The size is determined here through positive and negative numbers and 0 values. Instead of writing it directly if(a>b){returna;}elseretur

According to news on August 22, as the release of Samsung’s new generation flagship mobile phone S25 Ultra approaches, more and more details are beginning to emerge. The well-known blogger @ibinguniverse revealed more specifications of the S25 Ultra on Weibo today. The most eye-catching one is that its body width is the same as the Apple iPhone 16 Pro Max, both 77.6mm. 1. Thanks to Samsung’s further optimization of the frame design, the screen size of the S25 Ultra has been increased to 6.86 inches while maintaining the same width as the iPhone 16 Pro Max, providing users with a more immersive visual experience. The blogger further pointed out in the comment area that the black edges of the S25 Ultra are better than those of the iPhone 16 Pro Max&

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:
