I know how to find a (part of a) string and change that, but the problem is, the part I have to change is variable in length and in characters.
example
$string = "bla1bla2bla3textbla4bla5";
normaly I should explode to filter out text, but because "bla3" is different to "bla4", I have no idea how to explode or find "text", however it's always between "bla3" and "bla4", but "text" is variable aswel as bla1, 2 and 5.
after I find "text" I need to replace it by an other text.
Perhaps I can find this on google, but the problem is, I have no clue how to discribe it.
You can use preg_split() for this. I can't remember the exact regex for this, but it should be something like:
$text = preg_split("/[0-9]*/", "bla1bla2bla3textbla4bla5");
Using explode:
$parts = explode('text',$string); //splits it into two parts, removing `text`
$string = $parts[0] .'replacement'. $parts[1]; //glue the parts together with replacement
Using str_replace:
$string = str_replace('text','replacement');
And as for describing your problem: I'd try googling something like php replace substring.
ok, some examples.
$string1 = "bla1bla2bla3textbla4bla5";
find "text" in $string and replace it by "example"
output:
$string1 = "bla1bla2bla3examplebla4bla5";
other example
$string2 = "I bla3don'tbla4 know how to do it"
find "don't" in $string and replace it by "example"
output:
$string2 = "I bla3examplebla4 know how to do it"
other example
$string3 = "textbla3textbla4text"
find "text" but only the one between bla3 and bla4 and replace it by "example"
output shoud be something like
$string3 = "textbla3examplebla4text"
hope this makes it more clear.
so the part of bla3 and bla 4 stay the same, but anything else can change.
Related
How to eliminate\remove last character and space after that and to merge splitted word in php after using OCR for scanning documents
Tried with rtrim, replace etc..
But it also delete - on beginning of text
$delete = array('-');
if(in_array($string[(strlen($string)-1)], $delete))
$string = substr($string, 0, strlen($string)-1);
This is an example of text after ocr scanning
'Th- is is wh- at is looking like after doc- ument is scanned
-And it not look- ing good'
You know how it should be
This is what is looking like after document ....
Like I said I tried with replace but "-" sign is also removed from begging of text...
Idea is to remove "- " (dash and space) between splitted word and to marge word again
This can be accomplished with preg_replace.
$s = 'Th- is is wh- at is looking like after doc- ument is scanned -And it not look- ing good';
$s = preg_replace('/- /','',$s);
echo preg_replace('/ -/',". -\n",$s);
This is what is looking like after document is scanned.
-And it not looking good
A string is not an array in the strict sense, but there exists in-built php functions to convert one into another. They are explode() and implode()
The code below solves your problem.
<?php
$string = "Th- is is wh- at is looking like after doc- ument is scanned -And it not look- ing good";
//$delete = array('-');
$string_array = explode('- ',$string);
$string_new = implode($string_array);
echo $string_new;
I have a string of words in an array, and I am using preg_replace to make each word into a link. Currently my code works, and each word is transformed into a link.
Here is my code:
$keywords = "shoes,hats,blue curtains,red curtains,tables,kitchen tables";
$template = '%1$s';
$newkeys = preg_replace("/(?!(?:[^<]+>|[^>]+<\/a>))\b([a-z]+)\b/is", sprintf($template, "\\1"), $keywords);
Now, the only problem is that when I want 2 or 3 words to be a single link. For example, I have a keyword "blue curtains". The script would create a link for the word "blue" and "curtains" separately. I have the keywords separated by commas, and I would like the preg_replace to only replace the text between the commas.
I've tried playing around with the pattern, but I just can't figure out what the pattern would be.
Just to clarify, currently the output looks as follows:
shoes,hats,blue curtains,red curtains,tables,kitchen tables
While I want to achieve the following output:
shoes,hats,blue curtains,red curtains,tables,kitchen tables
A little bit change in preg_replace code and your job will done :-
$keywords = "shoes,hats,blue curtains,red curtains,tables,kitchen tables";
$template = '%1$s';
$newkeys = preg_replace("/(?!(?:[^<]+>|[^>]+<\/a>))\b([a-z ' ']+)\b/is", sprintf($template, "\\1"), $keywords);
OR
$newkeys = preg_replace("/(?!(?:[^<]+>|[^>]+<\/a>))\b([a-z' ']+)\b/is", sprintf($template, "\\1"), $keywords);
echo $newkeys;
Output:- http://prntscr.com/77tkyb
Note:- I just added an white-space in your preg_replace. And you can easily get where it is. I hope i am clear.
Matching white-space along with words is missing there in preg_replace and i added that only.
I have the following string and need to cut it into pieces. But how to do this correct?
The string: $sting = 0nl10000143947019,23000143947456,....,10450143947022
... means that more nrs can be in here
Now I want to cut down this string in the following parts:
$first contains the first nr or letter in the string. So in this case it should be: 0
$second contains the second and third nrs or letters in the string. So in this case it should be: nl
$third contains EVERYTHING after the third nr or letter in the string. So in this case it should be: 10000143947019,23000143947456,....,10450143947022
$fourth contains EVERYTHING after the last , in the string. Note the this nr can be bigger or smaller than in this excample. So in this case it should be: 10450143947022
Hope this is possible!
Kind regards
With a simple regexp
([0-9A-Za-z]{1})([0-9A-Za-z]{2})(.*)\,([^\,]+)$
Should work. In PHP this would work like
$string = "0nl10000143947019,23000143947456,23000143947456,10450143947022";
preg_match("#([0-9A-Za-z]{1})([0-9A-Za-z]{2})(.*)\,([^\,]+)$#i", $string, $matches);
// map $matches to the right variables. But you can do that yourself :-)
Regex should do it, here's something to get you started
preg_match("/^(\d+)([a-z]+)(.*?,([^,]*))$/i", $string, $m);
print_r($m);
See here:
http://ideone.com/Xk1oV
Try the following:
$first = substr($sting,0,1);
$second = substr($sting,1,2);
$third = substr($sting,3);
$fourth = end(explode(',',$sting));
When I've a string:
$string = 'word1="abc.3" word2="xyz.3"';
How can I replace the point with a comma after xyz in xyz.3 and keep him after abc in abc.3?
You've provided an example but not a description of when the content should be modified and when it should be kept the same. The solution might be simply:
str_replace("xyz.", "xyz", $input);
But if you explicitly want a more explicit match, say requiring a digit after the ful stop, then:
preg_replace("/xyz\.([0-9])+/", 'xyz\${1}', $input);
(not tested)
something like (sorry i did this with javascript and didn't see the PHP tag).
var stringWithPoint = 'word1="abc.3" word2="xyz.3"';
var nopoint = stringWithPoint.replace('xyz.3', 'xyz3');
in php
$str = 'word1="abc.3" word2="xyz.3"';
echo str_replace('xyz.3', 'xyz3', $str);
You can use PHP's string functions to remove the point (.).
str_replace(".", "", $word2);
It depends what are the criteria for replace or not.
You could split string into parts (use explode or preg_split), then replace dot in some parts (eg. str_replace), next join them together (implode).
how about:
$string = 'word1="abc.3" word2="xyz.3"';
echo preg_replace('/\.([^.]+)$/', ',$1', $string);
output:
word1="abc.3" word2="xyz,3"
If I have a description like:
"We prefer questions that can be answered, not just discussed. Provide details. Write clearly and simply."
And all I want is:
"We prefer questions that can be answered, not just discussed."
I figure I would search for a regular expression, like "[.!\?]", determine the strpos and then do a substr from the main string, but I imagine it's a common thing to do, so hoping someone has a snippet lying around.
A slightly more costly expression, however will be more adaptable if you wish to select multiple types of punctuation as sentence terminators.
$sentence = preg_replace('/([^?!.]*.).*/', '\\1', $string);
Find termination characters followed by a space
$sentence = preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', $string);
<?php
$text = "We prefer questions that can be answered, not just discussed. Provide details. Write clearly and simply.";
$array = explode('.',$text);
$text = $array[0];
?>
My previous regex seemed to work in the tester but not in actual PHP. I have edited this answer to provide full, working PHP code, and an improved regex.
$string = 'A simple test!';
var_dump(get_first_sentence($string));
$string = 'A simple test without a character to end the sentence';
var_dump(get_first_sentence($string));
$string = '... But what about me?';
var_dump(get_first_sentence($string));
$string = 'We at StackOverflow.com prefer prices below US$ 7.50. Really, we do.';
var_dump(get_first_sentence($string));
$string = 'This will probably break after this pause .... or won\'t it?';
var_dump(get_first_sentence($string));
function get_first_sentence($string) {
$array = preg_split('/(^.*\w+.*[\.\?!][\s])/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
// You might want to count() but I chose not to, just add
return trim($array[0] . $array[1]);
}
Try this:
$content = "My name is Younas. I live on the pakistan. My email is **fromyounas#gmail.com** and skype name is "**fromyounas**". I loved to work in **IOS development** and website development . ";
$dot = ".";
//find first dot position
$position = stripos ($content, $dot);
//if there's a dot in our soruce text do
if($position) {
//prepare offset
$offset = $position + 1;
//find second dot using offset
$position2 = stripos ($content, $dot, $offset);
$result = substr($content, 0, $position2);
//add a dot
echo $result . '.';
}
Output is:
My name is Younas. I live on the pakistan.
current(explode(".",$input));
I'd probably use any of the multitudes of substring/string-split functions in PHP (some mentioned here already).
But also look for ". " OR ".\n" (and possibly ".\n\r") instead of just ".". Just in case for whatever reason, the sentence contains a period that isn't followed by a space. I think it will harden the likelihood of you getting genuine results.
Example, searching for just "." on:
"I like stackoverflow.com."
Will get you:
"I like stackoverflow."
When really, I'm sure you'd prefer:
"I like stackoverflow.com."
And once you have that basic search, you'll probably come across one or two occasions where it may miss something. Tune as you run with it!
Try this:
reset(explode('.', $s, 2));