I've been trying to remove some text... between two sets of brackts... for two hours i have tried everything... i've been to existing questions here and the answers don't work for me... so here goes
what i have
[attachment=0:randomID]<!-- ia0 -->randomIMGnam.png<!-- ia0 -->[/attachment:randomID]
i really want to remove all of this from the beginning of a string i was able to remove everything inside the brackets but failed everytime to get rid of the image name
Yes this is from phpbb i've pulled it from my DB no problem but don't want it to be displayed when i echo it.
thanks in advance i really hope I really hope someone can help
edit: what i've tried
1. $extension_pos = strrpos($entry, '<!-- ia0 -->'); // find position of the last dot, so where the extension starts
$output = substr($entry, 0, $extension_pos) . '' . substr($entry, $extension_pos);
2.$output= preg_replace('#\].*?\[#', '', $entry);
$output = preg_replace('/\[[^]]*\]/', '', $entry);
$output explode(']', $entry);
$imagename = preg_replace('#([attachment.*?]).*?([/attachment.*?])#', '$1$2', $entry);
You can use this regex to replace:
$string = ' [attachment=0:randomID]<!-- ia0 -->randomIMGnam.png<!-- ia0 -->[/attachment:randomID]';
$string = preg_replace('/\[(.*?)\]/', '', $string);
You could use regular expression as in example:
<?php
$string = 'test [attachment=0:randomID]randomIMGnam.png[/attachment:randomID] test2 [something]
test3
[/something] test4';
echo preg_replace('#(\[(.*)\](.*)\[/.*\])#Us','',$string);
// output test test2 test4
?>
Using regex might be heavy for this kind of task.
You could instead use a simple reasoning, whenever you meet a open bracket increment a counter by one, whenever you meet a close bracket decrement the counter by one.
And as long as your counter is > 0 just ignore the characters.
Related
Through PHP I downloaded an array with product descriptions from the database, but when adding them to the csv file, they contain unnecessary newlines. Is it possible to somehow remove them without interfering with the database (from the php level) ?
I tried to edit according to advice, but unfortunately it does not turn out as it should. I tried str_replace () but also to no avail:
$file_open = fopen("file_empik.csv", "w");
fputcsv($file_open, $heders);
foreach($title_from_shopify_table as $info)
{
$descr = $info['descr'];
str_replace('\n', '', $descr);
print_r($descr);
$line_info = array('', $info['title'], $info['descr'], $info['sku'], $info['imgsrc'], '23%', $info['vendor']);
fputcsv($file_open, $line_info);
}
fclose($file_open);
return $products_data; ```
Thank you for any help
You can stock it to variable, then you'll be able to edit what you want.
Something like
$array = callFromDatabase
foreach($array as $line) {
$line.editMethod // What you want to do ...
}
You can use regex!
This replaces them with a space:
$string = trim(preg_replace('/\s\s+/', ' ', $string));
Here is more information on preg_replace:
https://www.w3schools.com/php/func_regex_preg_replace.asp
Edit: If you are simply wanting to store the data and mess around with it then you should use a variable as #Devinfo_dh has pointed out.
How can I get the 800-555 from this 800-555-5555 with explode()?
Here is a good example:
$rawPhoneNumber = "800-555-5555";
$phoneChunks = explode("-", $rawPhoneNumber);
First chunk = $phoneChunks[0]; //800
Second chunk = $phoneChunks[1]; //555
Third Chunk chunk = $phoneChunks[2]; //5555
But how can I get the 800-555?
Okay, I see, here need more comment... So, this is only an example... In real I add a word (now $word) to string delimiter and my string is a full article... I want that, if this word second time published in the article, with str_word_count() will count, how many characters was in the text to the second (or third, if I want that) $word...
So I want that, I get the string from the second "hit" to back.
Okay, here is a more obvious example:
$text = oh my god, thank you the lot of downvotes, geniuses *.*
$explode = explode(",", $text);
$whatiwant = $explode?? // I WANT THE STRING FROM THE SECOND "," TO BACK
So I want that $whatiwant = oh my god, thank you the lot of downvotes
Implode, explode and array_slice.
I use array_slice because that makes the function more dynamic.
Now you can just set the $items to get the number of items you want.
If you set a negative value it counts backwards.
$delim = ",";
$items =2;
$text = "oh my god, thank you the lot of downvotes, geniuses *.*";
$whatiwant = implode($delim, array_slice(explode($delim, $text),0,$items));
Echo $whatiwant;
https://3v4l.org/KNSC4
You could also have an start variable to make the start position dynamic.
https://3v4l.org/XD0NV
Doing concatenation of already generated array's indexes is the simple way for you.
Sample Code
echo $phoneChunks[0]."-".$phoneChunks[1];
This is working for me:
$rawPhoneNumber = "800-555-5555";
$phoneChunks = explode("-", $rawPhoneNumber);
$first_chunk = $phoneChunks[0]; //800
$second_chunk = $phoneChunks[1]; //555
$third_chunk_chunk = $phoneChunks[2]; //5555
$portion_array = array($first_chunk, $second_chunk);
echo implode("-",$portion_array);
Output:
800-555
I have this section some/aaa/9321/something from which I want to extract only 9321. "something" always differs, "aaa" is alyways static. So, I used:
$text = "something/aaa/9321/something";
$first = 'aaa/';
$after = '/';
preg_match("/$first(.*)$after/s",$text,$result);
echo $result;
But isn't working. Can somebody please tell me what I need to use?
I've tried this too:
$text = "something/aaa/9321/something";
$first = 'aaa';
preg_match("|$first(.*)|",$text,$result);
echo substr($result['1'], 1, 4);
But between aaa and something not always 4 characters.
Sorry for bad english. Thanks!
You should always preg_quote strings when you want them to be taken literally in a regular expression:
$text = 'something/aaa/9321/something';
$first = preg_quote('aaa/', '/');
$after = preg_quote('/', '/');
preg_match("/$first(.*)$after/s",$text,$result);
echo $result[1]; // '9321'
Demo
The problem was caused by the fact that / is the delimiter in your regex. You could have also solved this problem by using a different delimiter, such as ~, however, you would just run into the same problem as soon as your string had a ~ or any other character with a special meaning like ., or ?. By using preg_quote, you won't run into this problem again.
Have you tried escaping the /?
Instead of
$first = 'aaa/';
$after = '/';
try
$first = 'aaa\/';
$after = '\/';
What I'm trying to do is, if it exists, remove an occurrence of text inside a 'shortcode', eg: Here's some content [shortcode]I want this text removed[/shortcode] Some more content to be changed to Here's some content [shortcode][/shortcode] Some more content.
It seems like a pretty simple thing to do but I can't figure it out.. =/
The shortcode will only show up once in the entire string.
Thanks in advance for help.
Try this:
$var = "Here's some content [shortcode]I want this text removed[/shortcode] Some more content";
$startTag = "[shortcode]";
$endTag = "[/shortcode]";
$pos1 = strpos($var, $startTag) + strlen($startTag);
$pos2 = strpos($var, $endTag);
$result = substr_replace($var, '', $pos1, $pos2-$pos1);
It's very easy to do with preg_replace(). For your purpose, use /\[shortcode\].*\[\/shortcode\]/ as pattern.
$replace = "[shortcode][/shortcode]";
$filteredText = preg_replace("/\[shortcode\].*\[\/shortcode\]/", $replace, $yourContent);
See http://php.net/manual/en/function.preg-replace.php for more details.
One can use strpos() to find the position of [substring] and [/substring] in your string and replace the text with a whitespace via substr_replace()
if you do not want to bother with regular expessions:
if you do have the [shortcode] tag inside the string, than it is really no problem: just use a nested use of substr:
substr($string,0,strpos($string,'[substring]')+11)+substr($string,strpos($string,'[/substring]'),strlen($string))
where the first substr cuts the string to the start of the string to cut and the second adds the remaining stuff of the string.
see here:
http://www.php.net/manual/en/function.substr.php
http://www.php.net/manual/en/function.strpos.php
use regex in php to get rid of it.
preg_replace (shortcode, urText, '', 1)
$string = "[shortcode]I want this text removed[/shortcode]";
$regex = "#\[shortcode\].*\[\/shortcode\]#i";
$replace = "[shortcode][/shortcode]";
$newString = preg_replace ($regex, $replace, $string, -1 );
$content = "Here's some content [shortcode]I want this text removed[/shortcode] Some more content to be changed to Here's some content [shortcode][/shortcode] Some more content";
print preg_replace('#(\[shortcode\])(.*?)(\[/shortcode\])#', "$1$3", $content);
Yields:
Here's some content [shortcode][/shortcode] Some more content to be changed to Here's some content [shortcode][/shortcode] Some more content
there are more words in a text, some are english, some are latins, now, how to use preg_replace, broken all the links with #? make something like:
flow to the next => flow to the next ? (only broken the links with # in a long text.
Thanks.
not work for this.
$new = preg_replace('/<a(?:.*?)(href="#)(?:.*?)>(.*?)<\/a>/is', '$2', $old);
// this will also broken other links...
<?php
$old = 'flow to the next ';
$new = preg_replace('/(<a href="\#.*?">)(.*?)<\/a>/is', '$2', $old);
echo $new;
demo