strstr

Using strstr inside a switch php

谁都会走 提交于 2019-12-05 10:43:06
I just cannot think of the code. I have waay too many if statments which I want to change to be a switch statement, but I cannot find of the logic. At the moment I have: if(strstr($var,'texttosearch')) echo 'string contains texttosearch'; if(strstr($var,'texttosearch1')) echo 'string contains texttosearch1'; if(strstr($var,'texttosearch2')) echo 'string contains texttosearc2h'; //etc etc... But how can I achieve the same within a switch? switch (true) { case strstr($var,'texttosearch'): echo 'string contains texttosearch'; break; case strstr($var,'texttosearch1'): echo 'string contains

strstr() function

笑着哭i 提交于 2019-12-05 06:02:40
I am trying to write a program that compares a substring that the user enters with an array of strings. #include <stdio.h> #include <string.h> char animals[][20] = { "dogs are cool", "frogs are freaky", "monkeys are crazy" }; int main() { char input[10]; puts("Enter animal name: "); fgets(input, sizeof(input), stdin); int i; for(i = 0; i < 3; i++) { if(strstr(animals[i], input)) printf("%s", animals[i]); } return 0; } When I enter frogs, for example it should print the message "frogs are freaky", but it prints out nothing. So I tried to write a line to print out the value of the strstr()

Obtain first line of a string in PHP

有些话、适合烂在心里 提交于 2019-12-03 05:31:52
问题 In PHP 5.3 there is a nice function that seems to do what I want: strstr(input,"\n",true) Unfortunately, the server runs PHP 5.2.17 and the optional third parameter of strstr is not available. Is there a way to achieve this in previous versions in one line? 回答1: here you go $str = strtok($input, "\n"); strtok() Documentation 回答2: It's late but you could use explode. <?php $lines=explode("\n", $string); echo $lines['0']; ?> 回答3: $first_line = substr($fulltext, 0, strpos($fulltext, "\n")); or

How to find text between two strings in c

别说谁变了你拦得住时间么 提交于 2019-12-03 04:06:28
I need to extract the text between 2 string patterns in c. Example: aaaaaa<BBBB>TEXT TO EXTRACT</BBBB>aaaaaaaaa PATTERN1=<BBBB> PATTERN2=</BBBB> Thanks. Here is an alive example of how to do this #include <stdio.h> #include <string.h> int main(void) { const char *s = "aaaaaa<BBBB>TEXT TO EXTRACT</BBBB>aaaaaaaaa"; const char *PATTERN1 = "<BBBB>"; const char *PATTERN2 = "</BBBB>"; char *target = NULL; char *start, *end; if ( start = strstr( s, PATTERN1 ) ) { start += strlen( PATTERN1 ); if ( end = strstr( start, PATTERN2 ) ) { target = ( char * )malloc( end - start + 1 ); memcpy( target, start,

Obtain first line of a string in PHP

南楼画角 提交于 2019-12-02 20:06:14
In PHP 5.3 there is a nice function that seems to do what I want: strstr(input,"\n",true) Unfortunately, the server runs PHP 5.2.17 and the optional third parameter of strstr is not available. Is there a way to achieve this in previous versions in one line? Your Common Sense here you go $str = strtok($input, "\n"); strtok() Documentation It's late but you could use explode. <?php $lines=explode("\n", $string); echo $lines['0']; ?> $first_line = substr($fulltext, 0, strpos($fulltext, "\n")); or something thereabouts would do the trick. Ugly, but workable. try substr( input, 0, strpos( input, "

Agner Fog's strstr() vs g++ built-in strstr() performance

三世轮回 提交于 2019-12-02 07:10:28
I attempted to compare the built-in strstr() in g++ (g++ (Debian 7.2.0-19) with the assembly implementation in Agner Fog's asmlib ( http://www.agner.org/optimize/asmlib.zip ). The target CPU is core-i3 supporting sse4x and avx. To compile: g++ -O2 -msse4 main.cpp libaelf64.a (you would need the static library is from the asmlib distribution). Functions rdtsc64_start(), rdtsc64_end() to measure performance follow intel's white paper ( https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/ia-32-ia-64-benchmark-code-execution-paper.pdf ) Provided the technique is correct, A

Does strstr()'s implementation in gcc and VS have linear complexity?

孤街浪徒 提交于 2019-12-01 21:04:26
I know that there are fast string search algorithms, like Boyer–Moore and Knuth–Morris–Pratt , that have O(n+m) complexity, while the trivial solution would be O(n*m). So does the implementation of strstr() for the most popular toolchains - gcc and Visual Studio - use these fast O(n) algorithms, or does it use the trivial solution? GCC's runtime library uses Two-Way Algorithm which performs 2n-m text character comparisons in the worst case. It is O(n) complexity in the search phase but it needs a extra preprocessing phase which is O(m) complexity. You could find details on http://www-igm.univ

A pure bytes version of strstr?

断了今生、忘了曾经 提交于 2019-12-01 14:18:42
问题 Is there a version of strstr that works over a fixed length of memory that may include null characters? I could phrase my question like this: strncpy is to memcpy as strstr is to ? 回答1: memmem, unfortunately it's GNU-specific rather than standard C. However, it's open-source so you can copy the code (if the license is amenable to you). 回答2: Not in the standard library (which is not that large, so take a look). However writing your own is trivial, either directly byte by byte or using memchr()

wrong parameter count for strstr()

懵懂的女人 提交于 2019-12-01 09:29:35
I have built a nav menu in wordpres using a posts GUID, and post title, I am taking only part of the title and to do this I am doing the following, $casestudylist .= "<li class='subnav'><a href=".$v->guid.">". strstr($v->post_title, ":", true)."</a></li>"; however I get the following warning and cannot work out why: wrong parameter count for strstr() Basically I am trying to pull all the characters out of a string if they are before a : . hakre The PHP version you're using does not support the third parameter of strstr Docs , hence the error message. Your usage of the function requires PHP 5.3

wrong parameter count for strstr()

对着背影说爱祢 提交于 2019-12-01 07:24:29
问题 I have built a nav menu in wordpres using a posts GUID, and post title, I am taking only part of the title and to do this I am doing the following, $casestudylist .= "<li class='subnav'><a href=".$v->guid.">". strstr($v->post_title, ":", true)."</a></li>"; however I get the following warning and cannot work out why: wrong parameter count for strstr() Basically I am trying to pull all the characters out of a string if they are before a : . 回答1: The PHP version you're using does not support the