This seems to be anal fissure which is a painful linear tear or crack in the distal anal canal. The initiating factor can be trauma from the passage of a hard or painful bowel movement. Constipation too predisposes for it. Low-fiber diets, such as those lacking in raw fruits and vegetables, are associated with the development of anal fissures.
The mainstay of the treatment is to relieve the constipation and soften the motions/stools. Following would help;
1) stool-bulking agents, such as fiber supplementation
2) stool softeners
4) sitz baths
5) Nitroglycerine ointment for local application at anal canal.
Long term prevention of the constipation and keeping the stools soft will prevent the recurrence.
Intractable anal fissure which don't respond to the medical treatment may warrant for surgical therapy like anal stretching or botulinum toxin injection.
Please feel free for your follow up questions.
I am sorry, I forgot to add this link;
Please read the details of anal fissure on the above link.